[抄题]:

给定一个字符串,找到最多有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. Linux运维学习笔记-角色知识总结

    角色通过UID和GID区分 root:超级管理员,拥有所有权限,UID(0). 普通用户:拥有操作自己家目录下的所有权限,其他文件及目录(/etc./var)只有读的权限,UID(500-65535) ...

  2. Java第五次作业--面向对象高级特性(抽象类和接口)

    一.学习要点 认真看书并查阅相关资料,掌握以下内容: 掌握抽象类的设计 掌握接口的设计 理解简单工厂设计模式 理解抽象类和接口的区别 掌握包装类的应用 掌握对象的比较方法和比较器的使用 学习使用日期操 ...

  3. 【LGR-051】洛谷9月月赛

    [LGR-051]洛谷9月月赛 luogu 签到题 description 给出\(K\)和质数\(m\),求最小的\(N\)使得\(111....1\)(\(N\)个\(1\))\(\equiv k ...

  4. C# zedgraph 怎么设置初始时坐标轴的比例??

    http://bbs.csdn.net/topics/390872329 已解决,,,我问是刷新图用的,,我以为mypane.YAxis.Scale.Min=0; mypane.YAxis.Scale ...

  5. windows server 2012 AD 域和站点部署系列

    http://blog.csdn.net/ronsarah/article/category/1495599 http://blog.csdn.net/david_520042/article/cat ...

  6. docker基于commit命令创建支持ssh服务的镜像

    以centos为基础,目的使用ssh服务远程连接docker容器. 环境:宿主机centos7(宿主机ip地址为192.168.164.130),直接搜索docker的centos镜像,下载最新版本. ...

  7. debian8下pgsql的主备同步热切手动脚本

    9以后,通过流复制直接做 主:192.168.1.111 从:192.168.1.222 需要在postgres帐号下先配置ssh互信,双机都配置 sh-keygen -t rsa ssh-copy- ...

  8. httpclient的几种请求URL的方式

    一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...

  9. 微信小程序从子页面退回父页面时的数据传递 wx.navigateBack()

    我们知道,在微信小程序中,从一个页面转到另一个页面,一般情况下可以通过navigate或redirect时候的url来携带参数,然后在目标页面的onLoad函数参数中获取这些url参数.例如: // ...

  10. [Python] numpy.Matrix

    import numpy as np np.matrix('1, 2; 3, 4') #1, 2 #3, 4 np.matrix([[1,2],[3,4]]) #1, 2 #3, 4