[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 characters.
Example 1:
Input: "eceba"
Output: 3
Explanation: t is "ece" which its length is 3.
Example 2:
Input: "ccaabbb"
Output: 5
Explanation: t is "aabbb" which its length is 5.
题意:
给定字符串,找出至多包含两种字符的最长子串。
思路:
j
“e c e b a”
i e-0
i c-1
i e-2
-------map.size()<= 2
i b-3 此时移动 j
代码:
class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
if(s.length() < 2) return s.length();
HashMap<Character, Integer> map = new HashMap<>();
int result = 0;
int j = 0;
for(int i = 0; i < s.length(); ){
char c = s.charAt(i);
if(map.size() <= 2){
map.put(c, i);
i++;
}
if(map.size() > 2){
int leftMost = s.length();
for(int n : map.values()){
leftMost = Math.min(leftMost, n);
}
map.remove(s.charAt(leftMost));
j = leftMost + 1;
}
result = Math.max(result, i - j );
}
return result;
}
}
[leetcode]159. Longest Substring with At Most Two 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] 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 求两个字母组成的最大子串长度 --------- 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 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- [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 ...
- LeetCode 340. Longest Substring with At Most K Distinct Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/ 题目: Give ...
- 【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 ...
随机推荐
- 最简单的TCP、UDP案例及各函数的详细解释
TCP: server #include "stdafx.h" #include<iostream> #define BUF_SZIE 64 #include &quo ...
- mysql 笔记分享
mysql LPAD 和RPAD不足位数补齐填充函数总结一下mysql数据库的一些特征MySQL WHERE 语句优化之我见mysql limit 实例详解mysql 如何实现多表联合更新MySQL ...
- python带参数装饰器使用
# -*- coding: utf-8 -* """TensorFlow指定使用GPU工具类 author: Jill usage: 方法上加@tf_with_devic ...
- PyQt5系列教程
PyQt5系列教程(一)Mac OS X下搭建Python3.5.1+PyQt5开发环境PyQt5系列教程(二)利用QtDesigner设计UI界面PyQt5系列教程(三)用py2exe进行程序打包P ...
- 逆地址解析协议RARP
解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...
- api 1.1构架篇
首先让其自动加载??? 在YiiBase.php里面 改写autoload方法: /** * Class autoload loader. * This method is p ...
- Oracle JOB简例
JOB declare jobno number; begin dbms_job.submit( jobno, 'insert into tmptable1 values(1,1);', to_dat ...
- 1. SVN clean失败解决方法
svn执行clean up后出现提示:svn cleanup failed–previous operation has not finished; run cleanup if it was int ...
- Linux 配置TomCat 项目三大步骤
一: 安装 JRE 01: 下载 server-jre 安装包 => http://www.oracle.com/technetwork/java/javase/downloads/serve ...
- HTML5 Canvas ( 图形变换, 升级版的星空 ) translate, rotate, scale
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...