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 ...
随机推荐
- UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...
- 原创: 做一款属于自己风格的音乐播放器 (HTML5的Audio新特性)
灵感的由来是前些天看到了博: http://www.cnblogs.com/li-cheng 的首页有一个很漂亮的播放器,感觉很不错,是用Flex做的Flash播放器. 于是我也便想到了,自己也来来弄 ...
- php.ini配置中文详解
;;;;;;;;;;; ; 警告 ; ;;;;;;;;;;; ; 此配置文件是对于新安装的PHP的默认设置. ; 默认情况下,PHP使用此配置文件安装 ; 此配置针对开发目的,并且*不是*针对生产环境 ...
- 【 D3.js 视频系列 】 飞速入门
本教程共包含 6 个视频,目的是为了帮助初学者快速入门,以便阅读本站其他文章. 本教程的名称为"飞速入门",是为初学者准备的,其中包括了 D3 开发中最基础的知识.对 D3 掌握得 ...
- 【 D3.js 高级系列 — 8.0 】 标线
有时候,需要在地图上绘制连线,表示"从某处到某处"的意思,这种时候在地图上绘制的连线,称为"标线". 1. 标线是什么 标线,是指地图上需要两个坐标以上才能表示 ...
- 【Java学习笔记】函数使用
package aaa; public class aaa { public static int add(int a,int b) { return a+b; } public static voi ...
- 【C#学习笔记】播放wma/mp3文件
using System; using System.Runtime.InteropServices; namespace ConsoleApplication { class Program { [ ...
- test chemes
rcmobile://messages rcmobile://badge rcmobile://dialer rcmobile://open rcmobile://sms?type=new
- IOS 使用CoreText实现表情文本URL等混合显示控件
实现了一个富文本视图控件.主要针对表情图片,文本字符,URL,等这种类型的文本进行显示. 源码地址 https://github.com/TinyQ/TQRichTextView 实现的效果如下图. ...
- IOS 五星评分控件
程序中需要打分的功能,在网上找了几个,都不是很满意.下面是实现出的效果.可以点击,可以拖动. 使用方法:初始化控件. TQStarRatingView *starRatingView = [[TQSt ...