438.滑动窗口,找出所有字串返回索引

2018-03-15  本文已影响0人  Ching_Lee

···
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> list=new ArrayList<Integer>();
//长度一定,从左往右依次滑动窗口
for(int left=0,right=p.length()-1;right<s.length();left++,right++){
int[] freq=new int[256];
//设置p的每个字母的freq为1
for(int i=0;i<p.length();i++)
freq[p.charAt(i)]++;
//用来记录还有几个值就满足了
int count=p.length();
for(int k=left;k<=right;k++)
{
if(freq[s.charAt(k)]>=1)
{
freq[s.charAt(k)]--;
count--;
}

           if(count==0)
               list.add(left);
           
       }
       
   }
    
    return list;
}

}
···

上一篇 下一篇

猜你喜欢

热点阅读