最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)
[抄题]:
给定一个字符串,找到最多有k个不同字符的最长子字符串。eg:eceba, k = 3, return eceb
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 怎么想到两根指针的:从双层for循环的优化 开始分析
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 没有养成好习惯:退出条件写在添加条件之前。因此先判断if (map.size() == k),再map.put(c,1)
[二刷]:
- 没有养成好习惯:循环过后更新max,循环过后移动指针j,都要在循环之前就写好
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
[复杂度]:Time complexity: O(2n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 字符串中的字母用256[]存比较方便,但是用hashmap存也可以,相同的字母放在一个盒子里,盒子个数达到k时就退出
- 而且hashmap中还有.size()取总长度 .remove()去除key,城会玩 头一次见
[关键模板化代码]:

j用的是while循环,因为第二层不是for,for的特点是必须要从0开始
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
k = 2
[代码风格] :
hashmap判断有没有key不是用contains,而是用containsKey,没怎么注意
public class Solution {
/**
* @param s : A string
* @return : The length of the longest substring
* that contains at most k distinct characters.
*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
//corner case
int maxLen = 0;
HashMap<Character,Integer> map = new HashMap<>();
if (k > s.length()) {
return 0;
}
int i = 0, j = 0;
//i, j go in the same direction
for (i = 0; i < s.length(); i++) {
//put j into hashmap
while (j < s.length()) {
char c = s.charAt(j);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
}else {
if (map.size() == k) {
break;
}else {
map.put(c, 1);
}
}
//pointers move first
j++;
}
//renew ans first
maxLen = Math.max(max, j - i);
//remove i if exists
char c = s.charAt(i);
if (map.containsKey(c)) {
int count = map.get(c);
if (count > 1) {
map.put(c, count - 1);
}else {
map.remove(c);
}
}
}
return maxLen;
}
}
最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)的更多相关文章
- [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 ...
- [Swift]LeetCode159.具有最多两个不同字符的最长子串 $ 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 ...
- [Swift]LeetCode395. 至少有K个重复字符的最长子串 | Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [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] 395. 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] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- [leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
- [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 ...
随机推荐
- HDU 2292
http://acm.hdu.edu.cn/showproblem.php?pid=2292 题意:1-n个节点,题目给出了完全二叉树的定义(这个定义似乎有歧义,此题以题目描述为准),且要保持最小堆性 ...
- Linux运维学习笔记-定时任务知识总结
定时任务编辑规范流程: 重要知识点: 切记用全路径编写定时脚本.定时任务 大部分在 crontab 计划任务中都会年到未尾带 >/dev/null 2>&1,是什么意思呢? > ...
- 使TextView里面的文字滚动起来
要使TextView里面的内容滚动起来,那么就要配置好几个参数, 1.focusable来获取焦点. 2.ellipsize来获得滚动的方法. 3.focusableInTouchMode来获取触摸方 ...
- 如何在CentOS7上安装MySQL并实现远程访问
传送门 本人乃学生小白一枚,近期在学习Linux,所以就简单记录一下~ 安装MySQL 首先,需要检查一下是否已经安装了MySQL : # rpm -qa | grep mysql 这时候没有任何输出 ...
- 浅谈SQL Server---1
浅谈SQL Server优化要点 https://www.cnblogs.com/wangjiming/p/10123887.html 1.SQL Server 体系结构由哪几部分组成? 2.SQL ...
- swagger api 转graphql npm 包试用
graphql 比较方便的进行api 的查询,操作,swagger 是一个方便的open api 描述标准,当前我们有比较多的 restapi 但是转换为graphql 是有成本的,还好swagger ...
- macOS --- 配置基于域名的虚拟主机
在终端运行 sudo vi /Applications/XAMPP/xamppfiles/etc/httpd.conf,打开apache配置文件. 在httpd.conf中找到"#Inclu ...
- pthread中取消线程
取消线程:告诉一个线程关掉自己,取消操作允许线程请求终止其所在进程中的任何其他线程.不希望或不需要对一组相关的线程执行进一步操作时,可以选择执行取消操作.取消线程的一个示例是异步生成取消条件. 对于c ...
- Spring Bean单例与线程安全
一.Spring单例模式及线程安全 Spring框架中的Bean,或者说组件,获取实例的时候都是默认单例模式,这是在多线程开发的时候需要尤其注意的地方. 单例模式的意思是只有一个实例,例如在Sprin ...
- Jquery中的has、find、filter方法区别
find方法 find返回的是匹配结果集,作用于后代$(‘li’).find(‘.a’).css(‘background-color’, ‘red’);在li下面查找元素是否有class=a的元素,返 ...