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 ...
随机推荐
- 如何从ERP下载Sales BOM到CRM
在ERP使用事务码CS01创建一个BOM,类型选择5 - Sales BOM: BOM的抬头维护material 1419,在BOM的component部分维护另外两个material 1421和14 ...
- Android(java)学习笔记51:ScrollView用法
1. 理论部分 (1)ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 (2)上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 (3)上述两个控件可以互 ...
- Android(java)学习笔记30:泛型接口的概述和使用
1. 泛型接口的概述和使用: package cn.itcast_06; /* * 泛型接口:把泛型定义在接口上 */ public interface Inter<T> { public ...
- AFN 切换BaseUrl
在某个特定的接口需要修改baseurl时: 直接使用kvc: [_sessionManager setValue:[NSURL URLWithString:NEW_BASE_URL] forKey:@ ...
- 实验吧之损坏的U盘
1.首先用binwalk查看里面的内容,发现里面有Zip文件. 要想把Zip文件弄出来有两种方法: 一是用虚拟机里面的foremost+文件名 然而,在终端中已经见到二零password文件夹,然而我 ...
- SpringBoot 使用(三): 配置文件详解
代码从开发到测试要经过各种环境,开发环境,测试环境,demo环境,线上环境,各种环境的配置都不一样,同时要方便各种角色如运维,接口测试, 功能测试,全链路测试的配置,hardcode 肯定不合适,如S ...
- [译]GLUT教程 - 安装
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Setup 你需要什么 要用GLUT库开发程序,你可以下载最新版本3. ...
- ffmpeg一些filter用法、以及一些功能命令
来源:http://blog.csdn.net/dancing_night/article/details/46776903 1.加字幕 命令:ffmpeg -i <input> -fil ...
- 与select2有关的知识点总结
1.多选下拉框设置提示 var datass = [ { id:0, text: '你好' }, { id:1, text: '好久不见' }, { id:2, text: '好想你' } ]; va ...
- iOS | FMDB快速上手
任何的开发都或多或少的接触到数据库,而在IOS中一般使用的是SQLite数据库,这是一个轻量功能较为不错的数据库.而现在用到比较多的第三方数据库操作框架就是FMDB.废话不多说,相信查找到这篇文章的都 ...