[抄题]:

给定一个字符串,找到最多有k个不同字符的最长子字符串。eg:eceba, k = 3, return eceb

[暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 怎么想到两根指针的:从双层for循环的优化 开始分析

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 没有养成好习惯:退出条件写在添加条件之前。因此先判断if (map.size() == k),再map.put(c,1)

[二刷]:

  1. 没有养成好习惯:循环过后更新max,循环过后移动指针j,都要在循环之前就写好

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(2n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 字符串中的字母用256[]存比较方便,但是用hashmap存也可以,相同的字母放在一个盒子里,盒子个数达到k时就退出
  2. 而且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(没提交)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. fiddler模拟限速实战

    原理:Fiddler的模拟限速是在客户端请求前来自定义限速的逻辑,此逻辑是通过延迟发送数据或接收的数据的时间来限制网络的下载速度和上传速度,从而达到限速的效果. 算法:那么我们的算法就是 1000/下 ...

  2. 基于zookeeper简单实现分布式锁

    https://blog.csdn.net/desilting/article/details/41280869 这里利用zookeeper的EPHEMERAL_SEQUENTIAL类型节点及watc ...

  3. Python Logger使用

    1. 单文件的logging配置 import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filen ...

  4. Python 修饰符, 装饰符

    1, 看到@时候, 程序已经开始执行了. 所以@实际上是立即执行的 2, @后面的跟着函数名, 该函数(f1)是之前定义过的.  再后面跟着一个函数(f2), f2是f1的入口. 那么执行顺序是,  ...

  5. table 如何给tr border颜色

    border-collapse属性值 说明 separate 默认值,边框分开,不合并 collapse 边框合并,如果相邻,则共用一个边框 table,th,td{border:1px solid  ...

  6. bzoj 3924 [Zjoi2015]幻想乡战略游戏——动态点分治(暴力移动找重心)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3924 度数只有20,所以从一个点暴力枚举其出边,就能知道往哪个方向走. 知道方向之后直接走到 ...

  7. volatile的本质

    1. 编译器的优化 在本次线程内, 当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后,再取变量值时,就直接从寄存器中取值:当变量值在本线程里改变时,会同时把变量的新 ...

  8. java 面向对象 — 类和对象

    构造方法 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回值 5.构造器 ...

  9. linux编程基本

    库的使用头文件:.h 里面的函数及变量的声明 比如#include <stdio.h> ,Linux下默认头文件的搜索路径 系统定义的头文件: /usr/include /usr/loca ...

  10. Spring batch学习 详细配置解读(3)

    第一篇讲到普通job 配置 那么spring  batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等. 下面看一个动态设置读取文件的配置 1.动态文件读取 <?x ...