标签:list lis ram else add col color pre anagrams
1 class Solution { 2 public List<Integer> findAnagrams(String s, String p) { 3 List<Integer> res = new ArrayList<>(); 4 if(s.length() == 0 || s.length() < p.length()) return res; 5 int[] map = new int[128]; 6 int count = 0; 7 for(char c : p.toCharArray()){ 8 map[c]++; 9 count++; 10 } 11 int begin = 0, end = 0; 12 while(end < s.length()){ 13 if(end < p.length()-1){ 14 if(map[s.charAt(end)]-- > 0 ) count--; 15 end++; 16 }else{ 17 if(map[s.charAt(end)]-- > 0) count--; 18 end++; 19 if(count == 0) res.add(begin); 20 // System.out.println(count); 21 if(map[s.charAt(begin)]++ >= 0) count++; // >= 0 !!! 注意 22 begin++; 23 } 24 } 25 return res; 26 27 } 28 }
438. Find All Anagrams in a String
标签:list lis ram else add col color pre anagrams
原文地址:https://www.cnblogs.com/goPanama/p/9829255.html