LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical sliding window problem.
class Solution {
public:
int lengthOfLongestSubstringKDistinct(string s, int k) {
unordered_map<char, unsigned> hm;
int ret = , start = ;
for (int i = ; i < s.length(); i++)
{
hm[s[i]]++;
if (hm.size() <= k)
{
ret = std::max(ret, i - start + );
}
else
{
while (start < i && hm.size() > k)
{
char nc = s[start];
if (hm[nc] == ) hm.erase(nc);
else hm[nc]--;
start++;
}
}
}
return ret;
}
};
LeetCode "Longest Substring with At Most K Distinct Characters"的更多相关文章
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- LeetCode Longest Substring with At Most Two Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [Swift]LeetCode340.最多有K个不同字符的最长子串 $ Longest Substring with At Most K Distinct Characters
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
随机推荐
- Xen虚拟机磁盘镜像模板制作(三)—CentOS 7
这里整理下制作Xen CentOS 7磁盘镜像模版的流程: 1.创建一个将要用来安装CentOS 7系统的LV,命令如下: [root@localhost ~]# lvcreate -L 5G -n ...
- Java 中的数组操作
前言 在Java中,有很多封装好的类可以用来操纵数组(排序,复制等等),使得数组使用起来非常的方便.这就是高级语言带来的好处. 代码示例 - 一维数组 package test; import jav ...
- JS基础知识(作用域/垃圾管理)
1.js没有块级作用域 if (true) { var color = “blue”; } alert(color); //”blue” for (var i=0; i < 10; i++){ ...
- 光流算法:Brox算法(转载)
参考论文:1. High Accuracy Optical Flow Estimation Based on a Theory for Warping, Thomas Box, ECCV20042. ...
- Android多分辨率适配经验总结
Android多分辨率适配是一件很有意义但是比较麻烦的事情,网上有很多关于多分辨率适配的文章,多数文章讲解的都是整个APP的图片比较规则,可以将图片做成9图来完成多分辨率适配,但是对于一些游戏类应 ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Kernel panic - not syncing: Attempted to kill init
解决方法:系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜单,选择“kernel /vmlinuz-2.6.23.1-42.fc8 ro root=/dev/vogroup00/log ...
- java多线程:并发包中ConcurrentHashMap和jdk的HashMap的对比
一:HashMap--->底层存储的是Entry<K,V>[]数组--->Entry<K,V>的结构是一个单向的链表static class Entry<K, ...
- BufferedInputStream/BufferedOutputStream复制文件
public class Test{ public static void main(String[] args) throws IOException{ FileInputStream in = n ...
- dump java
http://www.gamlor.info/wordpress/2011/09/visualvm/ https://visualvm.java.net/zh_CN/gettingstarted.ht ...