[leetcode]Longest Substring with At Most Two Distinct Characters
2014-11-26 15:08
453 查看
很明显的2 pointer问题。。。当满足条件的时候后面的指针加,不满足条件的时候前面的指针加,直到满足条件。。。
class Solution { public: int lengthOfLongestSubstringTwoDistinct(string s) { int start = 0, cnt = 0; int char_set[256] = {0}; int ans = 0; for (int i = 0; i < s.size(); i++) { if (char_set[s[i]]++ == 0) cnt++; while (cnt > 2) { char_set[s[start]]--; if (char_set[s[start++]] == 0) cnt--; } ans = max(i - start + 1, ans); } return ans; } };
相关文章推荐
- 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
- Leetcode Longest Substring with At Most Two Distinct Characters
- LeetCode "Longest Substring with At Most Two Distinct Characters"
- [Leetcode] Longest Substring with At Most Two Distinct Characters
- leetcode 159: Longest Substring with At Most Two Distinct Characters
- [LeetCode160]Longest Substring with At Most Two Distinct Characters
- [LeetCode] Longest Substring with At Most Two Distinct Characters
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
- Leetcode-Longest Substring with At Most Two Distinct Characters.
- [Leetcode]Longest Substring with At Most Two Distinct Characters
- Leetcode: Longest Substring with At Most Two Distinct Characters
- leetcode[159] Longest Substring with At Most Two Distinct Characters
- [Leetcode] 159. Longest Substring with At Most Two Distinct Characters 解题报告
- LeetCode – Refresh – Longest Substring with At Most Two Distinct Characters
- leetcode[159]Longest Substring with At Most Two Distinct Characters
- [LeetCode] Longest Substring with At Most Two Distinct Characters
- [LeetCode] Longest Substring with At Most Two Distinct Characters
- Leetcode 159 Longest Substring with At Most Two Distinct Characters
- LeetCode - Longest Substring with At Most Two Distinct Characters