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 ...
随机推荐
- ContentControl与ContentPresenter区别?
<TextBlock HorizontalAlignment="Left" Text="{Binding HwContent, Converter={StaticR ...
- [USACO] 铺放矩形块 题解
题目大意: 给定4个矩形块,找出一个最小的封闭矩形将这4个矩形块放入,但不得相互重叠.所谓最小矩形指该矩形面积最小. 思路: 枚举矩形的安放顺序,再按照题目所给的图判断即可,主要要想到枚举. 代码: ...
- HDU5769 Substring(后缀数组)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5769 #include <iostream> #include <stdio.h> ...
- CI框架入门1
CI框架入门: 1.url的特点 2.目录结构/布局 3.MVC分别在哪里,如何依葫芦画瓢 4.安全性 ...
- IOS网络第二天 - 04-黑酷-GDataXML 解析
****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...
- 2016总结 wjwdive
2016 成长:收获最大的,学会了耐心,学会了宽容,学会了不强求.一念放下,万般自在.我真的是晚熟啊 ^_^! . 读书:<小王子>.<了不起的盖茨比>.<和任何人都聊得 ...
- kafka模块概述
简介 kafka主要用于实现低延迟的发送和收集大量的事件和日志数据--通常是活跃的数据(PV.访问记录等),数据以日志形式记录下来,然后由一个专门的系统来进行日志的收集与统计: 吞吐量极高的分布式消息 ...
- Eclipse下使用GDT插件无法登陆GAE & GDT无法上传JAVA代码
今天更新github主页的过程中,想使用GAE部署一个Java Web服务来更好的支持网站动态性(关键是利用了免费的GAE资源),结果遇到了2个大问题. 1.GDT插件无法登陆GAE账户 错误1:登陆 ...
- 1.Maven的安装以及本地仓库的配置
安装: maven下载地址:http://maven.apache.org/release-notes-all.html 然后解压,配置环境变量 MAVEN_HOME,并添加到path中.验证是否 ...
- 深入解析PHP中逗号与点号的区别
大部分同学都知道逗号要比点号快,但就是不知道为什么,更不知道逗号与点号这两者之间到底有什么区别.下面小编就来详细的为大家介绍一下,需要的朋友可以过来参考下 echo 'abc'.'def'; //用点 ...