【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 longest substring T that contains at most two distinct characters.
For example,
Given S = “eceba”,
T is "ece" which its length is 3.
Intuition
方法一:假设有一个滑动窗口,窗口左右两边的下标分别记为left和right。遍历字符串,依次确定left和right。具体实现见代码(稍微难理解一点)
方法二:还是引入一个滑动窗口,窗口左右两边的下标分别记为left和right。再引入一个numDistinct变量,用于记录窗口中已经出现的不同字符次数。此外,使用一个int[258]来模拟哈希表,记录每个字符出现的次数count。
遍历字符串(right右移),当遇到的字符出现次数为0时,numDistinct++; 当numDistinct的次数超过2时,说明left需要往右移,在left往右移的过程中,减少所指字符的count,最终使得numDistinct=2,才暂停left的右移。
方法二容易理解,且可以用于求解k个不同字符组成的最长子字符。下面代码中方法二就是实现k个不同字符的情况。
Solution
//Method1: Longest Substring with At Most Two Distinct Characters
public static int lengthOfLongestSubstringTwoDistinct1(String s) {
if(s==null || s.length()<=0)
return 0;
int left=0;
int right=-1;
int maxLen=0;
for(int i=1;i<s.length();i++) {
if(s.charAt(i)==s.charAt(i-1)) continue;
if(right!=-1 && s.charAt(i)!=s.charAt(right)) {
maxLen=Math.max(maxLen, i-left);
left=right+1;
}
right=i-1;
}
return Math.max(s.length()-left,maxLen);
} //Method2: Longest Substring with At Most k Distinct Characters
public static int lengthOfLongestSubstringTwoDistinct2(String s,int k) {
if(s==null || s.length()<=0)
return 0;
int[] count=new int[256];
int numDistinct=0;
int maxLen=0;
int left=0;
for(int right=0;right<s.length();right++) {
if(count[s.charAt(right)]==0) numDistinct++;
count[s.charAt(right)]++;
while(numDistinct>k) {
count[s.charAt(left)]--;
if(count[s.charAt(left)]==0) numDistinct--;
left++;
}
maxLen=Math.max(maxLen, right-left+1);
}
return maxLen;
}
Complexity
Time complexity : O(n)
Space complexity : O(1)
What I've learned
1. 方法一中,i可能会越界,所以代码第16行别忘记了s.length()-left。
2. 方法一里的left,right以及 i指针之间的关系要搞清楚。
3.方法二中引入了numDistinct,非常方便,有时候往往多设置一个变量,就能实现所需要的功能
More:【目录】LeetCode Java实现
【LeetCode】159. Longest Substring with At Most Two Distinct Characters的更多相关文章
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- [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】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- ✡ 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 ...
随机推荐
- Java——关于num++和++num
public class num_add_add { public static void numAdd(){ int num = 10; int a = num++; System.out.prin ...
- 20155205 2016-2017-2 《Java程序设计》第5周学习总结
20155205 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 如果没有try的话,出现异常会导致程序崩溃,而try则可以保证程序的正常运行下去.( ...
- Android学习笔记——Content Provider(一)
Content Provider是Android系统四大组件之一: 官方的定义是:A Content Provider manages access to a central repository o ...
- HashMap、ArrayMap、SparseArray分析比较
http://blog.csdn.net/chen_lifeng/article/details/52057427
- python正则表达式二[转]
原文:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一 ...
- SHA1算法原理
一.SHA1与MD5差异 SHA1对任意长度明文的预处理和MD5的过程是一样的,即预处理完后的明文长度是512位的整数倍,但是有一点不同,那就是SHA1的原始报文长度不能超过2的64次方,然后SHA1 ...
- 写好shell脚本的13个技巧【转】
有多少次,你运行./script.sh,然后输出一些东西,但却不知道它刚刚都做了些什么.这是一种很糟糕的脚本用户体验.我将在这篇文章中介绍如何写出具有良好开发者体验的 shell 脚本. 产品的最终用 ...
- linux添加定时任务crond
1.crontab –e:编辑当前定时任务 保存完重新crond : service crond restart 2. crontab用法 crontab –e : 修改 crontab 文件,如果文 ...
- C# 多种方式连接Oracle。
废话不多说直接正题: 首先我们先在Oracle数据库下建了一个用户叫做lisi,密码为lisi,在这个用户下建立一张表叫做“USERS”,在这个表下新增三个数据. 方式一:利用OleDb连接Oracl ...
- nagios系列(六)之nagios实现对服务器cpu温度的监控
1.安装硬件传感器监控软件sensors yum install -y lm_sensors* 2.运行sensors-detect进行传感器检测 ##一路回车即可 Do you want to ov ...