一开始以为是不连续的,其实要求子串是连续的。
想法: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的更多相关文章

  1. LeeCode(No3 - Longest Substring Without Repeating Characters)

    题目: Given a string, find the length of the longest substring without repeating characters. 示例: Given ...

  2. [Algorithm] Longest Substring Without Repeating Characters?

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  3. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  4. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  5. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  8. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. Random类(java.util)

    转自 Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字. 相同种子数的Rand ...

  2. Python试卷

    3.写一个函数,计算一个给定的日期是该年的第几天. def getday(self,y=None,m=None,d=None): date = datetime(y,m,d) days = date. ...

  3. MacOS10.9获取Android源码不完全笔记(2014)

    第一步:安装Macports 这个我就不叙述了,网上有无数教程 第二步:创建一个磁盘镜像 1.打开磁盘工具,然后: 第三步:使用Macport安装编译环境 1.打开终端输入以下内容 sudo port ...

  4. div中加入iframe,可以实现Ajax的功能

    div中加入iframe,可以实现Ajax的功能,如果查询的时候,比如说城市的选择,用Ajax去实现, 在.net 可以考虑用UpdatePanel,但是点击了查询刷新表格(表格需要重新刷新加载,非纯 ...

  5. 3DSoftRenderer

    研究了好几天基本的图形学,对于光栅化的大致过程有点了解了,很感谢网上的很多大牛的无私奉献,我就写一下这几天的总结,希望也能对网络上的知识做出一点点点的贡献. 屏幕有什么特点,无非是一排排的像素点,每个 ...

  6. EXTJS 4.2 资料 控件之checkboxgroup的用法(静态数据)

    1.页面 1.1点击‘横幅’,需要动态显示隐藏文本框 { xtype: 'fieldset', title: '指定附加图&横幅设置', collapsible: true, items: [ ...

  7. 隐藏和显示效果js动画

    <div id='ctt' style='margin-left: 50px; color: white'>             <input type="button ...

  8. python 记录日志logging

    在项目开发中,往往要记录日志文件.用python记录日志有两种方式: 1.利用python 自带的logging库,例如: # -*- coding: utf-8 -*- import osimpor ...

  9. mybatis(1):入坑篇

    依赖 <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artif ...

  10. ExtJS4.2学习(三)Grid表格(转)

    鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2013-11-07/172.html --------------- ...