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 ...
随机推荐
- 不知还有人遇到这个问题没有:数据库 'xxx' 的版本为 706,无法打开。此服务器支持 661 版及更低版本。不支持降级路径。
一般情况是要给数据库升级 但我一直在百度看看有没有不动低版本数据库的方法 终于...发现..可能别人发现,但我没查到的 我可以用一个更高版本的数据库打开,然后生成脚本,然后把脚本拿出来
- 自己用的框架写了一个PHP模版解析类
<?php if(!defined('IS_HEARTPHP')) exit('Access Denied'); /** * template.class.php 模板解析类 * * @copy ...
- 批量添加-fno-objc-arc
http://syxiaqj.github.io/2014/02/28/bee-learning-1/ 4.批量添加-fno-objc-arc 因为BeeFramework是一个非RAC的框架,现在X ...
- <一>SQL优化1-4
第一条:去除在谓词列上编写的任何标量函数 --->在select 显示列上使用标量函数是可以的.但在where语句后的过滤条件部分对列使用函数,需要考虑.因为执行sql的引擎会因为 ...
- Delphi Unable to invoke Code Completion due to errors in source code
这时因为在.pas文件中存在delphi无法识别的编码,也就是说.pas文件中的字符并非是纯粹的可由文本文件编辑器所能识别的编码.所以,delphi就不可能有效地解释这些编码.因而就出现了自动代码提示 ...
- MVC ActionResult -- JavaScriptResult,JsonResult
以下是ActionResult的继承图: 大概的分类: EmptyResult:表示不执行任何操作的结果 ContentResult :返回文本结果 JavaScriptResult:返回结果为Jav ...
- 最好最实用的PHP二次开发教程
◆二次开发 1.什么是二次开发? 二次开发,简单的说就是在现有的软件上进行定制修改,功能的扩展,然后达到自己想要的功能和效果,一般来说都不会改变原有系统的内核. 2.为什么要二次开发? 随着信息化技术 ...
- GCC 编译优化指南
转自: http://www.jinbuguo.com/linux/optimize_guide.html GCC 编译优化指南 作者:金步国[www.jinbuguo.com] 版权声明 本文作者是 ...
- POJ 1860 Currency Exchange
题意:有n种货币,可以互相兑换,有m个兑换规则,兑换规则给出汇率r和手续费c,公式为b = (a - c) * r,从a货币兑换为b货币,问能不能通过不断的兑换赚钱,兑换期间手中的钱数不可以为负. 解 ...
- 38、FragmentStatePagerAdapter分页
[ ViewPager ] ViewPager 如其名所述,是负责翻页的一个 View.准确说是一个 ViewGroup,包含多个 View 页,在手指横向滑动屏幕时,其负责对 View 进行切换.为 ...