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 ...
随机推荐
- javascript中的关键字和保留字
javascript中关键字的问题,将名称替换了下,确实就没有问题了.现在将它的关键字和保留字贴出来,便于日后查看和避免在次出现类似的问题. 1 关键字breakcasecatchcontinuede ...
- 继承在WCF中的问题和解决办法
1. 问题 假设有代码如下: [ServiceContract] public interface IA { [OperationContract] string M1(); } [ServiceCo ...
- 【C#学习笔记】获取当前应用程序所在路径及环境变量
转自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.Get ...
- LeetCode中有技巧的题需要面试前记得的
https://leetcode.com/problems/insert-interval/ http://www.cnblogs.com/yxzfscg/p/4459173.html https:/ ...
- jvm内部现成运行
hi,all 最近抽时间把JVM运行过程中产生的一些线程进行了整理,主要是围绕着我们系统jstack生成的文件为参照依据. 前段时间因为系统代码问题,造成性能瓶颈,于是就dump了一份stack出来 ...
- Delphi or函数的用法
function GetFlag(a: string): Integer;var I: Integer;begin Result := 0; for I := 0 to 3 - 1 do begin ...
- 锋利的jQuery读书笔记---jQuery中动画
jQuery中的动画: 1.show和hide 2.fadeIn和fadeOut 3.slideUp和slideDown <!DOCTYPE html> <html> < ...
- RMAN 备份详解
一.数据库备份与RMAN备份的概念 1.数据库完全备份:按归档模式分为归档和非归档 归档模式 打开状态,属于非一致性备份 关闭状态,可以分为一致性和非一致性 非归档模式 打开状态,非一致 ...
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...
- 消息提示和消息推送插件toastr
http://www.jq22.com/yanshi476 比较棒的消息提示和消息推送插件toastr function myIntervalshow() { // showPopup1(300, 1 ...