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 ...
随机推荐
- OOP——UML六种关系
UML定义的关系主要有:泛化.实现.依赖.关联.聚合.组合,这六种关系紧密程度依次加强,分别看一下 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述,具体描述建立在一般描述的基础之上,并对其 ...
- 调用DirectDraw接口和调DirectDraw7接口的不同点对比
调用DirectDraw接口步骤: 1. 包含链接库ddraw.lib 2. 初始化窗口类型(全屏独占时类型用popup). 3. 在初始化窗口后初始化Direct ...
- CentOS SVN服务器安装配置小记
SVN的安装 安装很简单,尤其对于CentOS这种,直接: # yum install subversion# yum install mod_dav_svn 不同发行版的Package安装方法参见h ...
- Android 超仿Path时间轴和扇形菜单的效果
网上看到的 ,仅此记录一下,用到的时候便于查找 效果如下: 源码下载地址 : http://download.csdn.net/detail/abc13939746593/7251933
- <五>面向对象分析之UML核心元素之边界
一:基本概念
- C# DataGridView的列对象属性探讨 (未完待续)
比较难的几个属性的释义[1]:
- Android http协议实现文件下载
用http协议下载文件,主要用到的是httpURLConnection对象,主要的步骤如下: 1. 创建HttpURLConnection对象 2.获得一个InputStream对象 3.修改权限:访 ...
- Dubbo入门实例--转载
原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool 1. 概述 Dubbo是一个分布式服务 ...
- linux中vi保存文件时的“Can't open file for writing”
今天在ubuntu 13.04环境下,使用vi新建一个文件,编辑保存时提示“Can't open file for writing”. 分析: 出现这个错误的原因可能有两个: 一是当前用户的权限不足: ...
- (原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡
(原创)LAMP教程5-配置VirtualBox虚拟机中centos6.4的网卡 是的,今天我们要讲的是如何配置VirtualBox虚拟机中centos6.4的网卡,毕竟我们是要做网站开发的,没有网络 ...