一开始以为是不连续的,其实要求子串是连续的。
想法: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. php调试利器 -- xdebug

    之前整理在印象笔记中,现在搬出来.分本地调试和远程调试.本文亲测通过并截图,有问题欢迎留言探讨. (参考网上多位前辈的博客,整理在笔记里忘记保存原文链接,这里无法贴出链接,望见谅)   # 痛处 一般 ...

  2. asp .NET弹出窗口 汇总(精华,麒麟创想)

    asp .NET弹出窗口 汇总(精华,麒麟创想) 注://关闭,父窗口弹出对话框,子窗口直接关闭 this.Response.Write("<script language=javas ...

  3. MySQL 5.1.63 单机配置多实例(简单配置)

    需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...

  4. 进程(Process)和线程(Thread)的关系和区别

    Definition定义-------------Process进程是应用程序的一次运行活动:从操作系统核 心角度来说,进程是操作系统分配和调度系统内存资源.cpu时间片等资源的基本单位,为正在运行的 ...

  5. SQL 跨服务器数据库增、删、改、查(二)

    --创建链接服务器 exec sp_addlinkedserver 'jx3xxiednr3ucidf', ' ', 'SQLOLEDB', 'jx3xxiednr3ucidf' exec sp_ad ...

  6. validate[.unobtrusive]和Bootstrap实现tooltip错误提示

    validate[.unobtrusive]和Bootstrap实现tooltip错误提示 类似的文章园子里已有,请看这里,个人感觉稍显复杂,日前也打算写一个简单的给项目用,一些关键点记录于此.最终效 ...

  7. appStore上传苹果应用程序软件发布流程(之前都是同事发,复制一份备用)

    首先确定帐号是否能发布, https://developer.apple.com/account,如果你打开Provisioning Portal,然后点击DisTribution看到的是下图中那样, ...

  8. ExtJS4.2学习(四)Grid表格中文排序问题(转)

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

  9. HDU 5039 Hilarity

    题意:一棵树n个结点,每条边有0.1两种权值,每次询问权值为奇数的路径数目,或者改变某一条边的权值. 分析:这个题目很巧妙低利用了异或和的特性,dfs得到每个点到根结点的权值异或和,然后奇数则为1,偶 ...

  10. linux的定时任务crontab

    每隔一分钟执行以下语句: #打印当前时间: date "+%Y-%m-%d %T" 保存为/usr/test/test.sh 查看系统中当前用户有多少个定时任务: crontab ...