Leetcode Longest Substring with At Most K Distinct Characters
来源:互联网 发布:淘宝主营业务是什么 编辑:程序博客网 时间:2024/05/16 06:11
题意:找出最长的含k个不同字符的子串的长度。
思路:windows slide
class Solution {public: int lengthOfLongestSubstringKDistinct(string s, int k) { if(k < 1) return 0; vector<int> alpha(128, 0); int b = 0, e = 0; int lengthMax = 0; while(true) { if(check(alpha, k)) { //cout << e << " " << b << endl; lengthMax = max(lengthMax, e - b); if(e < s.length()) alpha[s[e]] ++; else break; e ++; } else { alpha[s[b]] --; b ++; } } return lengthMax; } private: bool check(vector<int> alpha, int k) { int count = 0; for(int i = 0; i < alpha.size(); ++ i) { if(alpha[i] != 0) count ++; } return count <= k; }};
0 0
- LeetCode 340. Longest Substring with At Most K Distinct Characters
- leetcode Longest Substring with At Most K Distinct Characters
- Leetcode Longest Substring with At Most K Distinct Characters
- leetcode 340. Longest Substring with At Most K Distinct Characters
- lintcode:Longest Substring with At Most K Distinct Characters
- Longest Substring with At Most K Distinct Characters
- Longest Substring with At Most K Distinct Characters
- [Amazon] Longest Substring with At Most K 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
- 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 159 Longest Substring with At Most Two Distinct Characters
- [LeetCode]159. Longest Substring with At Most Two Distinct Characters
- jQuery
- 17.5.4 学习总结 段落方面
- 错误:Unable to instantiate receiver
- latex 绘图,在图上插入数学公式
- 关于css动画的优化
- Leetcode Longest Substring with At Most K Distinct Characters
- C语言+easyX界面库实现贪吃蛇
- 自动内存管理机制-垃圾收集器与内存分配策略
- Java中list对象的三种遍历方式
- LDAP 网络用户账户
- 我要努力!!!!!!!!
- OpenGL Superbible 7 02 Our First OpenGL Program
- win10手动编译opencv3.2+contrib
- a bit与a little 的区别