思路:看到题目首先想到最大字符串匹配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. UOJ 48 次最大公约数

    次最大公约数 = gcd / 其中一个数质因数中最小的. gcd(42,12) = 6;    div(42) = 2*3*7   div(12) = 2^2*3 sgcd(42,12) = 6 / ...

  2. 51nod 1442 士兵的旅行

    拆点,因为只能走一步,那么u->v 后就不能到k了,这样,建图就能保证只走一步: #include <bits/stdc++.h> using namespace std; *; c ...

  3. 深搜(DFS),回溯,Fire Net

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2 解题报告: 这里的深搜有一点不同,就是,在深搜每一个点时,都要深搜每 ...

  4. 2018.11.7 关于将Web项目部署到阿里云服务器-5个步骤搞定

    将Eclipse导出的War包部署到阿里云服务器上,提供给移动端实时的访问 1. 先登录阿里云网站注册账号,选择服务器类型(我用的是 云服务器ECS), 如果你还是在读大学生可享受优惠价,最低好像是9 ...

  5. 【Calculus 微积分の一些个人理解】

    微积分 微积分(Calculus)是高等数学中研究函数的微分(Differentiation).积分(Integration)以及有关概念和应用的数学分支.它是数学的一个基础学科.内容主要包括极限.微 ...

  6. 【luogu P3366 最小生成树】 模板

    这里是kruskal做法 当然prim也可以,至于prim和kruskal的比较: Prim在稠密图中比Kruskal优,Kruskal在稀疏图中比Prim优. #include<bits/st ...

  7. js标准对象——Date

    在JavaScript中,Date对象用来表示日期和时间. 要获取系统当前的时间: var now = new Date(); alert(now); now;//Mon Oct 23 2017 11 ...

  8. JavaScript高阶函数map/reduce、filter和sort

    map() 举例说明,比如我们有一个函数f(x)=x²,要把这个函数作用在一个数组[1,2,3,4,5,6,7,8,9]上. 由于map()方法定义在JavaScript的Array中,我们调用Arr ...

  9. Unity 游戏框架搭建 (十三) 无需继承的单例的模板

    之前的文章中介绍的Unity 游戏框架搭建 (二) 单例的模板和Unity 游戏框架搭建 (三) MonoBehaviour单例的模板有一些问题. 存在的问题: 只要继承了单例的模板就无法再继承其他的 ...

  10. Webpack学习笔记九 webpack优化总结

    webpack 优化笔记 webpack4 自带的优化包括 swingTree(摇摆树)和作用域提升 swingTree 比如入口文件 index.js引入通用方法 util, 里面有 10个方法, ...