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, ...
随机推荐
- 对vector等STL标准容器的排序操作
[+] STL提供的Sort 算法 所有sort算法介绍 sort 中的比较函数 sort 的稳定性 全排序 局部排序 nth_element 指定元素排序 partition 和stable_par ...
- Microsoft Visual Studio Ultimate 2013 旗舰版 有效注册密钥
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...
- [msf]那些年儿跑过的字典
SEC标签里都会说一些网络完全相关的,光说理论也不好,光将工具太肤浅,不做脚本小子,有一句话说的好,我们都知道最酷的是什么?酷的不是“h4ck3r”这两个字,而是技术. OK,-let's go!! ...
- SQL Server是如何让定时作业
如果在SQL Server 里需要定时或者每隔一段时间执行某个存储过程或3200字符以内的SQL语句时,可以用管理->SQL Server代理->作业来实现. 1.管理->SQL S ...
- 1037. Magic Coupon (25)
#include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...
- EXPLAIN PLAN获取SQL语句执行计划
一.获取SQL语句执行计划的方式 1. 使用explain plan 将执行计划加载到表plan_table,然后查询该表来获取预估的执行计划 2. 启用执行计划跟踪功能,即autotrace功能 3 ...
- Device eth0 does not seem to be present
解决办法: 首先,打开/etc/udev/rules.d/70-persistent-net.rules内容如下面例子所示: # vi /etc/udev/rules.d/70-persistent- ...
- 【 Quartz】使用 JobListener (任务监听器可实现) 我想在一个任务执行后在执行第二个任务怎么办呢
http://liuzidong.iteye.com/blog/1147528 Quartz之JobExecutionException 博客分类: Java Quartz quartzjobexec ...
- (转载)Cocos2dx-OpenGL ES2.0教程:初识MVP(3)
在上一篇文章中,我在介绍vertex shader的时候挖了一个坑:CC_MVPMatrix.它其实是一个uniform,每一个cocos2d-x预定义的shader都包含有这个uniform, 但是 ...
- netty sample
http://netty.io/wiki/ https://github.com/netty/netty/tree/master/example/src/main/java/io/netty/exam ...