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_algorithm/MinimumWindowSubString.java at master · rpj911/LeetCode_algorithm Expand file tree/ Copy pathMinimumWindowSubString.javaMore file actions 83 lines (68 loc) · 2.62 KB / Copy pathMinimumWindowSubString.javaFile metadata and controls83 lines (68 loc) · 2.62 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 package Algorithms; import java.util.HashMap; public class MinimumWindowSubString { public static void main(String[] strs) { System.out.println(MinimumWindowSubString.minWindow("a", "a")); } public static String minWindow(String S, String T) { if (S == null || T == null || T.length() == 0 || S.length() == 0) { return ""; } int resultLen = -1; int resultleft = 0; int sum = 0; HashMap<Character, Integer> setSub = new HashMap<Character, Integer>(); for (int i = 0; i < T.length(); i++) { if (setSub.containsKey(T.charAt(i))) { setSub.put(T.charAt(i), setSub.get(T.charAt(i)) + 1); } else { setSub.put(T.charAt(i), 1); } } // count the number of characters in String T; sum = setSub.size(); HashMap<Character, Integer> map = new HashMap<Character, Integer>(); // record the left board. int left = 0; // record the //int equal = sum; for (int i = 0; i < S.length(); i++) { if (setSub.containsKey(S.charAt(i))) { if (map.containsKey(S.charAt(i))) { map.put(S.charAt(i), map.get(S.charAt(i)) + 1); } else { map.put(S.charAt(i), 1); } if (map.get(S.charAt(i)) == setSub.get(S.charAt(i))) { sum--; } } // contains all the charaters. if (0 == sum) { while (true) { Integer number = map.get(S.charAt(left)); // it should contains at least the number in T if (number != null && number <= setSub.get(S.charAt(left))) { map.put(S.charAt(left), number - 1); sum++; break; } if (number != null) { map.put(S.charAt(left), number - 1); } left++; } if (resultLen == -1 || resultLen > i - left + 1) { resultLen = i - left + 1; resultleft = left; left++; } } } if (resultLen == -1) { return ""; } else { return S.substring(resultleft, resultleft + resultLen); } } } |
| You are visitor 01402520 — 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. |