程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

LeetCode之滑动窗口

发布于2022-01-06 09:01     阅读(844)     评论(0)     点赞(2)     收藏(1)


438. 找到字符串中所有字母异位词

题目描述

 思路:滑动窗口

 

具体代码

  1. class Solution {
  2. public List<Integer> findAnagrams(String s, String p) {
  3. int sLen = s.length(), pLen = p.length();
  4. if (sLen < pLen) {
  5. return new ArrayList<Integer>();
  6. }
  7. List<Integer> ans = new ArrayList<Integer>();
  8. int[] sCount = new int[26];
  9. int[] pCount = new int[26];
  10. for (int i = 0; i < pLen; ++i) {
  11. ++sCount[s.charAt(i) - 'a'];
  12. ++pCount[p.charAt(i) - 'a'];
  13. }
  14. if (Arrays.equals(sCount, pCount)) {
  15. ans.add(0);
  16. }
  17. for (int i = 0; i < sLen - pLen; ++i) {
  18. --sCount[s.charAt(i) - 'a'];
  19. ++sCount[s.charAt(i + pLen) - 'a'];
  20. if (Arrays.equals(sCount, pCount)) {
  21. ans.add(i + 1);
  22. }
  23. }
  24. return ans;
  25. }
  26. }



所属网站分类: 技术文章 > 博客

作者:听说你很拽

链接:http://www.javaheidong.com/blog/article/372935/61aa265d14b6583e5da0/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)