GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/src/FindAllAnagramsInAString.java at master · LeBW/leetcode Expand file tree/ Copy pathFindAllAnagramsInAString.javaMore file actions 48 lines (46 loc) · 1.22 KB / Copy pathFindAllAnagramsInAString.javaFile metadata and controls48 lines (46 loc) · 1.22 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.util.ArrayList; import java.util.List; /** * 438. Find All Anagrams In a String * 滑动窗口,哈希 * 要注意边界情况 * @author LBW */ public class FindAllAnagramsInAString { public List<Integer> findAnagrams(String s, String p) { if (s.length() < p.length()) { return new ArrayList<>(); } int[] pcount = new int[26]; for (char c: p.toCharArray()) { pcount[c - 'a'] += 1; } List<Integer> res = new ArrayList<>(); int i = 0, j = -1; int[] count = new int[26]; while (j < p.length() - 1) { j += 1; count[s.charAt(j) - 'a'] += 1; } do { if (equals(count, pcount)) { res.add(i); } count[s.charAt(i) - 'a'] -= 1; i += 1; j += 1; if (j < s.length()) { count[s.charAt(j) - 'a'] += 1; } } while (j < s.length()); return res; } private boolean equals(int[] count, int[] pcount) { for (int i = 0; i < count.length; i++) { if (count[i] != pcount[i]) { return false; } } return true; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |