LeetCode – Refresh – Minimum Window Substring
2015-03-21 08:03
417 查看
Notes:
1. When check left shifting, do not use continue, but break it!!! Otherwise, you wil fall into infinite loop
2. Initialize the map with T, not S!!!!
1. When check left shifting, do not use continue, but break it!!! Otherwise, you wil fall into infinite loop
2. Initialize the map with T, not S!!!!
class Solution { public: string minWindow(string S, string T) { int len = S.size(), count = 0, length = INT_MAX; string result; if (len == 0 || len < T.size()) return ""; unordered_map<char, int> dict; for (char c : T) { if (dict[c]) dict[c]++; else dict[c] = 1; } for (int left = 0, right = 0; right < len; right++) { if (dict.find(S[right]) != dict.end()) { dict[S[right]]--; if (dict[S[right]] == 0) { count++; } } while (left < len) { if (dict.find(S[left]) != dict.end() && dict[S[left]] >= 0) break; if (dict.find(S[left]) != dict.end()) { dict[S[left]]++; } left++; } if (count == dict.size() && length > (right - left + 1)) { result = S.substr(left, right-left+1); length = right - left + 1; } } return result; } };
相关文章推荐
- leetcode -- Minimum Window Substring -- 重点,应该会考
- Leetcode---Minimum Window Substring
- LeetCode 76 minimum window substring
- LeetCode 76 Minimum Window Substring(Python详解及实现)
- leetcode 76: Minimum Window Substring
- 【Leetcode】Minimum Window Substring
- Minimum Window Substring [leetcode]
- [Leetcode] - Minimum Window Substring
- 【leetcode】Minimum Window Substring
- LeetCode 笔记系列16.1 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
- LeetCode Minimum Window Substring
- LeetCode76 Minimum Window Substring(两种解法)
- [leetcode刷题系列]Minimum Window Substring
- LeetCode 76: Minimum Window Substring
- [leetcode] Minimum Window Substring
- Minimum Window Substring @LeetCode
- 【leetcode】Minimum Window Substring (hard) ★
- **LeetCode-Minimum Window Substring
- LeetCode: Minimum Window Substring
- leetcode: Minimum Window Substring