[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 characters.
For example, Given s = “eceba” and k = 2,
T is "ece" which its length is 3.
题意:
给定字符串,求至多包含K种字符的最长子串
思路:
跟[leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串思路大体相同
j
j
S= “e c e b a” and k = 2, return 3 for "e c e"
i e-0 map.size <=2 move i
i c-1 map.size <=2 move i
i e-2(update) map.size <=2 move i
--------- b-3 map.size >2 get the length then move j
i
代码:
 class Solution {
     public int lengthOfLongestSubstringKDistinct(String s, int k) {
         //corner
         if(s.length() < k ) return s.length();
         // general
         HashMap<Character, Integer> map = new HashMap<>();
         int j = 0;
         int result = 0;
         for(int i = 0; i < s.length(); ){
             char c = s.charAt(i);
             if(map.size() <= k){
                 map.put(c, i);
                 i++;
             }
             if(map.size() > k){
                 int leftMost = s.length();
                 for(int n : map.values()){
                     leftMost = Math.min(n, leftMost);
                 }
                 map.remove(s.charAt(leftMost));
                 j = leftMost + 1;
             }
             result = Math.max(i - j , result);
         }
         return result; 
     }
 }
[leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串的更多相关文章
- [leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串
		
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
 - [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
		
Given a string s , find the length of the longest substring t that contains at most 2 distinct char ...
 - 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
		
Longest Substring with At Most Two Distinct Characters Given a string, find the length of the longes ...
 - ✡   leetcode   159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度  --------- java
		
Given a string, find the length of the longest substring T that contains at most 2 distinct characte ...
 - leetcode[159] Longest Substring with At Most Two Distinct Characters
		
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
 - 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 Two Distinct Characters 最多有两个不同字符的最长子串
		
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
 
随机推荐
- 第15章 高并发服务器编程(1)_非阻塞I/O模型
			
1. 高性能I/O (1)通常,recv函数没有数据可用时会阻塞等待.同样,当socket发送缓冲区没有足够多空间来发送消息时,函数send会阻塞. (2)当socket在非阻塞模式下,这些函数不会阻 ...
 - 【转】SQL模糊查询
			
在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表示任 ...
 - 【Python编程:从入门到实践】chapter3 列表简介
			
chapter3 列表简介3.1 列表是什么 列表是一系列按特定顺序排列的元素组成. bicycle = ['trek','cannondale'] print bicycle 3.1.1 访问列表元 ...
 - MySQL 查看执行的SQL记录
			
我们时常会有查看MySQL服务端执行的SQL记录.在MySQL5.1之后提供了支持,通过在启动时加入-l 或者--log选项即可: mysqld -l mysqld --log 在后面的版本(5.1. ...
 - File处理
			
package com.cfets.ts.u.shchgateway.util; import java.io.BufferedInputStream; import java.io.Buffered ...
 - python连接数据库(pymysql)及数据库加密
			
内容: 1.pymysql介绍 2.pymysql基本使用 3.数据库加密 参考:http://www.cnblogs.com/wupeiqi/articles/5713330.html 1.pymy ...
 - Bogart BogartAutoCode.vb
			
Imports System.Data.SqlClient Imports System.Data Public Class BogartAutoCodeDataBase Private Conn A ...
 - 《GPU高性能编程CUDA实战》附录一 高级原子操作
			
▶ 本章介绍了手动实现原子操作.重构了第五章向量点积的过程.核心是通过定义结构Lock及其运算,实现锁定,读写,解锁的过程. ● 章节代码 #include <stdio.h> #incl ...
 - HTML5 位运算符
			
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
 - JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )
			
< 1 > 实体类 Person package java_spring.modle; /** * 一个实体类( Person ) */ public class Person { pri ...