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, ...
随机推荐
- 51nod1264线段相交
1264 线段相交 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出平面上两条线段的两个端点,判断这两条线段是否相交(有一个公共点或有部分重合认为相交). 如果相交, ...
- (转载)SQL语句,纵列转横列
SQL语句,纵列转横列 Feed: 大富翁笔记 Title: SQL语句,纵列转横列 Author: wzmbox Comments sTable.db库位 货物编号 库存数1 0101 501 01 ...
- 求小于等于n的所有素数
题目:要求输出所有小于等于n的素数(n>=2,且为正整数) 要求:1.每行输出10个素数 2.尽可能采用较优算法 #include<iostream> #include<cma ...
- Eclipse开发Android报错Jar mismatch! Fix your dependencies
常常打开工程,发现项目并没有错,但是会有一个红X,然后就生成不了. 发现两个版本不同的android-support-v4.jar在使用 打开window-show views-problems Ja ...
- PostgreSQL+PostGIS的使用 函数清单
一. PostgreSQL与PostGIS的关系 PostgreSQL 是世界上技术最先进的开源数据库,其前身是1977年一个源于Berkeley名为Ingres的非关系型数据库,其项目领导人为Mic ...
- Insist
1.怎么自动截断文本? 如题,当数据库中的数据内容超出了要显示的长度时,如果不采取措施,会破坏页面的布局美观,所以可以采用自动截断文本,需要查看的时候再把其他的内容显示出来. 没截断的时候如下图: 再 ...
- OO之装饰者模式
以下为装饰者模式详解: 引子: 假如有一个快餐店,基本种类分为米饭,水饺,粉面等,但每一种类型的快餐又可以搭配不同的料,如米饭可以点各种不同的菜(排骨,青菜,土豆等),如果按照一般的设计,快餐为基类, ...
- 【莫队】bzoj 3781,bzoj 2038,bzoj 3289
好像又有一个星期没更博客了.. 最近疯狂考试...唯一有点收获的就是学会了莫队这种神奇的算法.. 听起来很难..其实是一个很简单的东西.. 就是在区间处理问题时对于一个待求区间[L',R']通过之前求 ...
- ntpServer搭建用以进行时间同步
在试各种乱七八糟的集群中,突然发现了一个问题,假如在一个闭网环境下安装某些集群软件的时候服务器之间的时间不同步(如HBase),会导致启动失败.那么就需要进行时间同步.可是往常都是网络校准的,没网的集 ...
- ***CI异常记录到日志:CodeIgniter中设计一个全局exception hook
在CodeIgniter中,当发生异常时,经常要通知系统管理员,因此有必要在全局的高度上 捕捉异常,因此可以写一个hook, 比如在config目录的hook.php中,加入: $hook['pre_ ...