LeetCode:3.Longest Substring Without Repeating Characters

思路:看到题目首先想到最大字符串匹配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的更多相关文章
- Python 解leetcode:3. Longest Substring Without Repeating Characters
题目描述:求一个字符串的不含重复字符的最长连续子串的长度: 思路: 使用一个哈希表保存字符出现的位置: 使用left和right分别表示子串的最左和最右字符的下标: 遍历字符串,如果当前字符在哈希表中 ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- 用批处理在windows中导出/导入无线网络信息,复制保存为bat即可
@echo offtitle 在windows中导出/导入无线网络信息 :Beginecho ========================echo 请选择操作:echo 1 查看可用的无线网络ec ...
- IOS CoreLocation框架的使用(用于地理定位)
● 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 ● 导航:去任意陌生的地方 ● 周边:找餐馆.找酒店.找银行.找电影院 ● 在上述应用中,都用到了地图和定位功能,在iOS开发中 ...
- 用ant打包apkbuilder找不到了的解决办法
apkbuilder的情况下生成apk文件,其实apkbuilder是一个批处理文件,打开里面就能发现,其实他内部执行的是sdklib.jar里面的一个class,所以就知道怎么做了,很简单,我们自己 ...
- 2018.12.20 Spring环境如何搭建
Spring学习 1.导入spring约束 为后续创建xml文件做铺垫 2.开始搭建Spring环境 1.创建Web项目,引入spring的开发包(根据下面的图来引入) 2.引入jar包 coreCo ...
- GPU CUDA编程中threadIdx, blockIdx, blockDim, gridDim之间的区别与联系
前期写代码的时候都会困惑这个实际的threadIdx(tid,实际的线程id)到底是多少,自己写出来的对不对,今天经过自己一些小例子的推敲,以及找到官网的相关介绍,总算自己弄清楚了. 在启动kerne ...
- js 跳转整理
js方式的页面跳转1.window.location.href方式 <script language="javascript" type="text/java ...
- 【luogu P2762 太空飞行计划问题】 题解
题目链接:https://www.luogu.org/problemnew/show/P2762 算是拍照那个题的加强下. 输入真的很毒瘤.(都这么说但好像我的过了?) #include <qu ...
- HDU 1260 Tickets (普通dp)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1260 Tickets Time Limit: 2000/1000 MS (Java/Others) ...
- Java研究
Strap 箱线图 峰度 随机过程 马尔科夫 超几何分布 贝叶斯公式 随机变量 德摩根 功率谱 残差 吸收壁 平稳随机 chorst 深入JVM OSGI ...
- 使用终端命令行将本地项目上传到Github并提交代码
第一步: 在Github上创建自己的repository 第二步:建立本地仓库cd到你的本地项目根目录下,执行git命令 1:$ cd 到你的项目目录下 2:$ git init 第三步:将本地项目工 ...