思路:看到题目首先想到最大字符串匹配KMP算法

 public static int lengthOfLongestSubstring(String s) {
int maxLength = 0;
StringBuilder sb = new StringBuilder(s);
a:for(int i = 0;i<sb.length();i++){
StringBuilder sb2 = new StringBuilder("");
sb2.append(sb.substring(i,i+1));
b:for(int j=i+1;j<sb.length();j++){
c:for(int k =0;k<sb2.length();k++){
if(!sb.substring(j,j+1).equals(sb2.substring(k,k+1))){ }else{
if(maxLength<j-i)
maxLength = j-i;
break b;
}
if(k == sb2.length())
sb2.append(sb.substring(j,j+1));
}
}
}
return maxLength;
}

参考后代码

public static int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))){
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}

LeetCode:3.Longest Substring Without Repeating Characters的更多相关文章

  1. Python 解leetcode:3. Longest Substring Without Repeating Characters

    题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中 ...

  2. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  3. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  4. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  5. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

  6. 【LeetCode】3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. leetcode题解 3. Longest Substring Without Repeating Characters

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

  8. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  9. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. POSIX 线程详解(经典必看)

    http://www.cnblogs.com/sunminmin/p/4479952.html 总共三部分: 第一部分:POSIX 线程详解                               ...

  2. 广义mandelbrot集,使用python的matplotlib绘制,支持放大缩小

    迭代公式的指数,使用的1+5j,这是个复数.所以是广义mandelbrot集,大家能够自行改动指数,得到其它图形.各种库安装不全的,自行想办法,能够在这个站点找到差点儿全部的python库 http: ...

  3. [18/11/22] 将点分十进制的IP地址化成二进制输出

    #include <stdio.h> void binary(int d){ ,j,n,b[]={}; ){ n=d%; d=d/; b[i++]=n; //不停的除2,余数保存在b[8] ...

  4. PHP处理Excel

    今天一个朋友问我PHP怎么导出Excel,然后我就去网上搜刮了一下资料,发现不错的第三方类PHPExcel(可以导入导出)和PHP-ExcelReader(导入).那就给大家分享一下! 一.PHP导入 ...

  5. 使用strtus2框架的json插件来完成ajax操作

    ------------------------------------------------------------------------------jsp------------------- ...

  6. 超简单,快速修改Oracle10g的默认8080端口

    因为Oracle数据库默认的端口是8080,这也是tomcat服务器的默认端口. 为了避免端口冲突,我们通常会修改掉其中一个. 这里我们选择修改Oracle数据库的端口. 第一步:以管理员身份运行cm ...

  7. 使用maven创建项目

    http://192.168.4.112/rdmanager/main/index.jhtml 1.对于第一次下载某个项目的源码,按照下面的步骤进行: (1)在D:\projects\目录下的空白位置 ...

  8. Storm 出现 no jzmq in java.library.path

    在真实环境中运行时,在log日志下,查看workpid日志发现出现该错误. 解决办法: 在conf/storm.yaml添加jzmq安装的路径, 我使用的默认安装在/usr/local/lib下 ja ...

  9. JAVA面向对象思想理解分析

    1.面向对象是面向过程而言.两者都是一种思想.面向过程:强调的是功能行为.(强调过程.动作)面向对象:将功能封装进对象,强调了具备了功能的对象.(强调对象.事物)面向对象是基于面向过程的.将复杂的事情 ...

  10. Sonar安装-Linux[20171227]

    前言     一款不错的代码质量管理工具Sonar 前期准备     官方参考文档 https://docs.sonarqube.org/display/SONAR/Documentation     ...