340. Longest Substring with At Most K Distinct Characters
最后更新
二刷
08-Jan-2017
和76、159很像。。
2 pointers.. 通过判断是否每一次是有效读取来增减accumulator,从而判断当前是否符合规定,再来更新maxLength
Time: O(n)
Sapce : O(1)
public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
if (s.length() == 0 || k == 0) return 0;
int[] chars = new int[256];
int temp = 0;
int right = 0;
int maxLength = -1;
for (int left = 0; left < s.length(); left ++) {
while (right < s.length()) {
// next read will be valid read, will make cumulator ++, since we cannot
// tolerate more dinstince chars by temp == k, we shall break at thsi point
if (temp == k && chars[s.charAt(right)] == 0) {
break;
}
if (++chars[s.charAt(right++)] == 1) {
temp ++;
}
}
if (right - left > maxLength && temp <= k) {
maxLength = right - left;
}
if (--chars[s.charAt(left)] == 0) {
temp --;
}
}
return maxLength;
}
}
一刷
18-Dec-2016
做过一个很类似的,记不清了。
维持窗口,用map记录出现字符的最后位置,超过K的时候从最左删一个。。更新大小。。
这也是HARD难度的么。。
public class Solution
{
public int lengthOfLongestSubstringKDistinct(String s, int k)
{
if(k == 0 || s.length() == 0) return 0;
Map<Character,Integer> map = new HashMap<Character,Integer>();
int res = 1;
int temp = 0;
int l = 0;
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if(map.containsKey(c))
{
map.put(c,i);
}
else
{
map.put(c,i);
temp++;
if(temp > k)
{
char tempK = c;
int index = i;
Iterator iter = map.keySet().iterator();
while(iter.hasNext())
{
char key = (char)iter.next();
if(index > map.get(key))
{
tempK = key;
index = map.get(key);
}
}
map.remove(tempK);
l = index + 1;
temp--;
}
}
res = Math.max(res,i+1-l);
}
return res;
}
}
340. Longest Substring with At Most K Distinct Characters的更多相关文章
- [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 ...
- [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 Most K Distinct Characters && Summary: Window做法两种思路总结
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 ...
- 最多有k个不同字符的最长子字符串 · Longest Substring with at Most k Distinct Characters(没提交)
[抄题]: 给定一个字符串,找到最多有k个不同字符的最长子字符串.eg:eceba, k = 3, return eceb [暴力解法]: 时间分析: 空间分析: [思维问题]: 怎么想到两根指针的: ...
- Longest Substring with At Most K Distinct Characters
Given a string, find the longest substring that contains only two unique characters. For example, gi ...
随机推荐
- bzoj1927: [Sdoi2010]星际竞速
跟上一题几乎一样... #include<cstdio> #include<cstring> #include<iostream> #include<algo ...
- Java知识点:内部类
内部类class文件命名规则 普通内部类.静态内部类:<Outer>\$<Inner>.class,其中<Outer>为外部类类名,<Inner>为内部 ...
- Darwin Streaming Server 安裝操作備忘
Darwin Streaming Server 安裝操作 Darwin Streaming Server是蘋果公司推出的開放源碼.跨平台多媒體串流伺服器, 提供音樂 (mp3) 與影音 (3gp.mp ...
- LINUX启动ORACLE监听和服务
可通过secureCRT或者telnet直接连 启动监听命令:lsnrctl start 成功启动后:sqlplus /nolog 回车 conn / as sysdba 回车 startup 回车 ...
- 【Java】Java处理double相加的结果异常
方式一(四舍五入):保留两位小数 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, ...
- 【转】declare-styleable的使用(自定义控件) 以及declare-styleable中format详解
原文网址:http://www.cnblogs.com/622698abc/p/3348692.html declare-styleable是给自定义控件添加自定义属性用的 1.首先,先写attrs. ...
- 【转】linux下a.out >outfile 2>&1重定向问题
原文网址:http://blog.chinaunix.net/uid-25909722-id-2912890.html 转自:http://blog.chinaunix.net/space.php?u ...
- Android01--开发环境搭建
1 -- 下载所需软件 Android SDK下载地址:http://developer.android.com/sdk/index.html Eclipse下载地址:http://www.eclip ...
- 锋利的jQuery读书笔记---jQuery中动画
jQuery中的动画: 1.show和hide 2.fadeIn和fadeOut 3.slideUp和slideDown <!DOCTYPE html> <html> < ...
- Android-AnimationDrawable(二)
首先可以先定义一个逐帧播放的xml: <?xml version="1.0" encoding="utf-8"?> <animation-li ...