Given a string, find the length of the longest substring T that contains at most k distinct characters.

For example, Given s = “eceba” and k = 2,

T is "ece" which its length is 3.

我的做法:维护一个window,r移动到超出k distinct character限制是更新max,然后移动l使distinct character <=k; 这种做法更新max只发生在超出限制的时候,有可能永远都没有超出限制,所以while loop完了之后要补上一个max的更新

 public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int l=0, r=0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int longest = 0;
if (s==null || s.length()==0 || k<=0) return longest;
while (r < s.length()) {
char cur = s.charAt(r);
map.put(cur, map.getOrDefault(cur, 0) + 1);
if (map.size() > k) {
longest = Math.max(longest, r-l);
while (map.size() > k) {
char rmv = s.charAt(l);
if (map.get(rmv) == 1) map.remove(rmv);
else map.put(rmv, map.get(rmv)-1);
l++;
}
}
r++;
}
longest = Math.max(longest, r-l);
return longest;
}
}

别人非常棒的做法:sliding window,while loop里面每次循环都会尝试更新max,所以每次更新之前都是把l, r调整到合适的位置,也就是说,一旦r移动到超出K distinct char限制,l也要更新使k distinct char重新满足,l更新是在max更新之前

 public class Solution {
public int lengthOfLongestSubstringKDistinct(String s, int k) {
int[] count = new int[256];
int num = 0, i = 0, res = 0; //num is # of distinct char, i is left edge, j is right edge
for (int j = 0; j < s.length(); j++) {
if (count[s.charAt(j)]++ == 0) num++;
if (num > k) {
while (--count[s.charAt(i++)] > 0);
num--;
}
res = Math.max(res, j - i + 1);
}
return res;
}
}

Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结的更多相关文章

  1. [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 ...

  2. LeetCode "Longest Substring with At Most K Distinct Characters"

    A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  5. LeetCode Longest Substring with At Most Two Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/ 题目: Gi ...

  6. [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 ...

  7. LeetCode 340. Longest Substring with At Most K Distinct Characters

    原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Linux学习笔记(5)-hello world

    经过三天的熟悉,我已经将教程中那些常用命令都使用了一遍,所以,从今天起,我已经从一直Linux菜鸟蜕变成了大雕-- Linux的命令无穷多,要想背下来那肯定是不可能的,所以我的目标便是混个手熟,那些常 ...

  2. ZZULI 1876: 蛤玮的项链 Hash + 二分

    Time Limit: 6 Sec  Memory Limit: 128 MBSubmit: 153  Solved: 11 SubmitStatusWeb Board Description 蛤玮向 ...

  3. 如何禁止root用户远程登陆

    如果不禁止root用户的远程登陆,就会将root用户暴露在网络环境中, 因为在缺省的安装中root用户是一定存在的,所以root用户容易受到攻击, 所以我们可以禁止root用户的远程登陆来实现保护ro ...

  4. table常用功能总结

    1,设置表格边框为单线框 table, th, td { border: 1px solid blue; }加上:table { border-collapse:collapse; } 由于 tabl ...

  5. *HDU1598 并查集

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. python学习 第一天

    正式学习python第一天,网上找到了python教程,带练习题的,又装了ubuntu. 这是学习笔记: list[]: 可变,append/1,insert/2,pop/index? tuple() ...

  7. NGUI实现技能CD效果

    在NGUI中使用Sprite的遮罩效果可以很轻松的实现技能CD效果. 具体实现步骤: ①新建一个技能图标的Sprite 如图中的Skill001,再在该技能Sprite上添加一个Sprite做遮罩, ...

  8. iOS开发之CocoaLumberjack

    Cocoa LumberJack是一个功能强大的NSlog,是通用的Cocoa日志框架之一.它可以提供更高级的log功能,比如记录log至文件或网络,并可根据log的级别(info.debug.war ...

  9. android——从零开始

     一.JDK(不用安装)1.下载适合的jdk2.配置环境变量 添加一系统边=变量       JAVA_HOME=D:\Java\jdk1.8.0_91      CLASSPATH=.;%JAVA_ ...

  10. Java图片转换为base64格式

    /** * @Descriptionmap 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 * @author temdy * @Date 2015-01-26 * @param pa ...