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 characters. For example, Given s = “eceba” and k = 2, T is "ece" which its length is 3.
我的做法:维护一个window,r移动到超出k distinct character限制是更新max,然后移动l使distinct character <=k; 这种做法更新max只发生在超出限制的时候,有可能永远都没有超出限制,所以while loop完了之后要补上一个max的更新
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int l=0, r=0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int longest = 0;
if (s==null || s.length()==0 || k<=0) return longest;
while (r < s.length()) {
char cur = s.charAt(r);
map.put(cur, map.getOrDefault(cur, 0) + 1);
if (map.size() > k) {
longest = Math.max(longest, r-l);
while (map.size() > k) {
char rmv = s.charAt(l);
if (map.get(rmv) == 1) map.remove(rmv);
else map.put(rmv, map.get(rmv)-1);
l++;
}
}
r++;
}
longest = Math.max(longest, r-l);
return longest;
}
}
别人非常棒的做法:sliding window,while loop里面每次循环都会尝试更新max,所以每次更新之前都是把l, r调整到合适的位置,也就是说,一旦r移动到超出K distinct char限制,l也要更新使k distinct char重新满足,l更新是在max更新之前
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int[] count = new int[256];
int num = 0, i = 0, res = 0; //num is # of distinct char, i is left edge, j is right edge
for (int j = 0; j < s.length(); j++) {
if (count[s.charAt(j)]++ == 0) num++;
if (num > k) {
while (--count[s.charAt(i++)] > 0);
num--;
}
res = Math.max(res, j - i + 1);
}
return res;
}
}
Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结的更多相关文章
- [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"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- [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 ...
随机推荐
- mysql导入导出,及错误记录
进入mysql的bin目录,如果mysql的bin添加了环境变量则不用. 导出,不指定编码则默认为:utf8mb4.: mysqldump -u root -h 127.0.0.1 -P 3307 - ...
- Scrum项目8.0
目标:我们开始做每个需要细致的界面,比方说登陆,注册,游戏界面,难度选择 内容:登陆界面:登陆名,登陆密码,登陆按钮,以及优美的底图. 注册页面:名称,密码,登陆按钮,以及优美的底图. 游戏界面:难度 ...
- php 小函数
1 变量函数 a.is_xxx函数用来判断变量类型 is_numeric (PHP 4, PHP 5) — 检测变量是否为数字或数字字符串 is_int.is_integer.is_long,判断变 ...
- JavaScript调用函数的方法
摘要:这篇文章详细的介绍了Javascript中各种函数调用的方法及其原理,对于理解JavaScript的函数有很大的帮助! 一次又一次的,我发现,那些有bug的Javascript代码是由于没有真正 ...
- Gitbook简易教程
简介 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书.GitBook支持输出以下几种文档格式 静态站点:GitBook ...
- js模块开发(一)
现在嵌入页面里面的javascript代码越来越复杂,于是可能依赖也越来越严重,使用别人开发的js也越来越多,于是在理想情况下,我们只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块. 于是j ...
- HDU5769 Substring(后缀数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 #include <iostream> #include <stdio.h> ...
- 【5集iCore3_ADP演示视频】5-3 iCore3应用开发平台摸校准
iCore3双核心应用开发平台基于iCore3双核心板,包含ARM.FPGA.7寸液晶屏.双通道数字示波器.任意波发生器.电压表等模块,是一款专为电子爱好者设计的综合性电子学习系统. [视频简介]本视 ...
- js 打印
关于js打印很简单的一段代码 function doPrint() { var newWindow = window.open("打印窗口", "_blank" ...
- 记一次程序排错与std::getline
今天忙活了半个下午,查找正式环境上面一个程序的问题.这个程序的作用是监控文件夹,处理每一个文件,分析每个文件的每行记录,然后将这个文件拆分成两个结果文件投放到另外两个不同的目录下面去,当处理完这个文件 ...