[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 ...
随机推荐
- git修改用户名和邮箱
用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用户名和邮箱纪录. 1.查看用户名和地址 git config user.name git config us ...
- VS:error C3872: '0xe044': this character is not allowed in an identifier解决方法
从网上粘贴代码到编译器中直接编译的话,会报这个错误,但是代码看上去是没有问题的,实际的原因是因为我们粘贴代码的时候粘贴了中文字符进来. 解决方法:就是把这段代码放到记事本里,选择替换把中文输入空 ...
- HP-UX oracle RAC 双机实践 (转载)
一.软硬件配置检查 1.检查内存#/usr/contrib/bin/machinfoCPU info: 4 Intel(R) Itanium 2 9100 series processors (1. ...
- Python 基础 json 与pickle
json 支持: str,int,tuple,list,dictpickle 支持python里所有的数据类型(包括函数) 只能在python中使用 json 与pickle 是一种 ...
- uva579-简单计算题
题意: 求分钟和时钟之间的夹角 解法:俩个夹角互减 AC:10ms #include<iostream> #include<functional> #include<qu ...
- 配置WDS支持使用UEFI模式启动
使用WDS通过Legacy+MBR方式部署操作系统不难,网上文章也有很多,本文就不赘述了,主要记录一下通过UEFI+GPT方式部署. 网上文章虽然也有介绍通过UEFI+GPT方式部署,但大多数说的比较 ...
- windows2008r2共享文件夹设置方法
一,无法启用网络发现的方法 参考网站: http://www.jb51.net/os/windows/win2008/154631.html Function Discovery R ...
- 2 python第三章文件操作
1.三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: if 条件成立: val = 1 else: val = 2 改成三元运算: val = 1 if 条件成立 els ...
- ios 缩放动画
CABasicAnimation *buttonAni = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; ...
- Haskell语言学习笔记(30)MonadCont, Cont, ContT
MonadCont 类型类 class Monad m => MonadCont m where callCC :: ((a -> m b) -> m a) -> m a in ...