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 ...
随机推荐
- IOS 通讯录 (访问,添加,修改)
如何访问用户的通讯录 在iOS中,有2个框架可以访问用户的通讯录 AddressBookUI.framework 提供了联系人列表界面.联系人详情界面.添加联系人界面等 一般用于选择联系人 Add ...
- 一、OSI模型
OSI/RM协议是由ISO(国际标准化组织)制定的,它有三个基本的功能:提供给开发者一个必须的.通用的概念以便开发完善.可以用来解释连接不同系统的框架.OSI将计算机网络体系结构(architectu ...
- LG3690 【【模板】Link Cut Tree (动态树)】
题目 终于去写\(LCT\)了 这个大爷讲的挺好的 板子 #include<algorithm> #include<iostream> #include<cstring& ...
- Ubuntu不支持rpm安装软件解决方法
Ubuntu不支持rpm安装软件解决方法 以前经常使用的是RedHat Linux,习惯使用rpm方法安装软件.最近发现Ubuntu系统居然不支持rpm方法安装软件,提示信息如下: root@root ...
- Cesium.js学习第二天(立方体)
var viewer = new Cesium.Viewer('cs'); viewer.entities.add({//图标 position: Cesium.Cartesian3.fromDegr ...
- 指令过滤器orderBy
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- js箭头函数
ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一个箭头 x =>x*x 相当于: function(x) { ...
- 数据恢复顾问(DRA)
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文件和(某些环境下)控制文件的损坏,它不提供spfile和logfile的修复 ...
- c# 本地完整缓存组件
用了一段时间java,java实现服务端程序很简单,有很多公共开源的组件或者软件.但是c#的很少. 现在准备自己写点东西,学习下新的东西,总结下c#的内容以及我们经常用的内容,抽离成类,组件,模型.方 ...
- 利用login-path对MySQL安全加固
Preface Connection security is one of the most important safety strategies which we should ...