LeeCode Algorithm #3 Longest Substring Without Repeating Characters
一开始以为是不连续的,其实要求子串是连续的。
想法:two-pointer O(n)时间,再借助256大小的int数组。
两个下标i,j(i<=j)。
对于i=0,找到最右侧的字符不重复的下标的后一位j。这就是i=0为左边界的串的最优解。然后i++。即考察i=1为左边界的情况。
如果此时[i,j]的串仍存在重复,那么肯定不是最终的最优解,继续i++。否则,j持续增1直到出现字符重复,即寻找当前i为左边界的最优解。
public class Solution {
//two pointer
public int lengthOfLongestSubstring(String s) {
int len = s.length();
if( len==0 ) return 0;
int[] flag = new int[256];
int ret=1,i=0,j=0; flag[s.charAt(0)]=1;
while( (++j)<len ){
flag[s.charAt(j)]++;
if( flag[s.charAt(j)]>1 ){
ret = Math.max(ret, j-i);
while( flag[s.charAt(j)]>1 ){
flag[s.charAt(i++)]--;
}
}
}
ret = Math.max(ret, j-i);
return ret;
}
}
LeeCode Algorithm #3 Longest Substring Without Repeating Characters的更多相关文章
- LeeCode(No3 - Longest Substring Without Repeating Characters)
题目: Given a string, find the length of the longest substring without repeating characters. 示例: Given ...
- [Algorithm] Longest Substring Without Repeating Characters?
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- spring-cloud-turbine
turbine主要用于聚合hystrix的监控数据 依赖pom <dependencyManagement> <dependencies> <dependency> ...
- 用JS写的放大镜
代码如下 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- 【CSDN人物访谈】蒋守壮分享他的技术成长之路以及对Hive技术的解读与思考
结缘大数据技术 CSDN:请简单地介绍一下自己. 蒋守壮:首先非常感谢CSDN能够给我这次被专访的机会,可以让我重新审视自己的职业发展历程,也希望能够帮助一些同行的朋友们.目前就职万达网络科技集团有限 ...
- MS也遵守规范了
CSS学的好不好,就看你对浏览器的兼容性处理的好不好. 拿opacity来说,本来写成opacity:0.3就完事了,但MS不来这套,它用filter,我们就不得不专门为它而 加上这么一大串(file ...
- SYN, FIN, ACK, PSH, RST, URG
在TCP层,有个FLAGS字段,这个字段有以下几个标识:SYN, FIN, ACK, PSH, RST, URG. 其中,对于我们日常的分析有用的就是前面的五个字段. 它们的含义是: SYN表示建立连 ...
- python总字符串
前面我们讲解了什么是字符串.字符串可以用''或者""括起来表示. 如果字符串本身包含'怎么办?比如我们要表示字符串 I'm OK ,这时,可以用" "括起来表示 ...
- Qt编译postgreSQL驱动
安装postgreSQL,安装目录下的lib和bin添加到path 打开Qt安装目录,找到src\plugins\sqldrivers\psql编辑psql.pro,添加INCLUDEPATH += ...
- 友盟分享在appdelegate中的调用语句举例
//友盟 [UMSocialData setAppKey:UmengAppkey]; [UMSocialConfig setSupportedInterfaceOrientations:UIInter ...
- shell复习笔记----用户管理
$ who 可以知道系统上有多少登陆 $who |wc -l 计算用户个数 注意:|是管道符号,可以在两个程序之间建立管道(pipeline):who 的输出,成了 wc 的输入, wc 所列出 ...
- 为什么要在<button>元素中添加type属性
在HTML中<button> 标签定义一个按钮. <button type="button">Click Me!</button> 在 butt ...