3. Longest Substring Without Repeating Characters寻找不重复的最大子串
首先弄清楚Substring和Subsequence,前者是子串,要求连续,后者是子序列,可以不连续
public int lengthOfLongestSubstring(String s) {
/*
一开始自己想的方法:两个指针,一个作为开始,一个用来遍历,复杂度O(N)
*/
int l = s.length();
if (l==0)
return 0;
char[] str = s.toCharArray();
int res = 1;
Set<Character> set = new HashSet<>();
for (int i = 0; i < l-1; i++) {
set.add(str[i]);
for (int j = i+1; j < l; j++) {
if (set.contains(str[j]))
break;
set.add(str[j]);
res = Math.max(res,set.size());
}
set.clear();
}
return res;
}
public int lengthOfLongestSubstring2(String s) {
/*
O(N)做法,自己建立哈希表来记录有没有出现过,第一次接触这种做法,要记住
思路就是用哈希表记录当前字符最近一次出现时的index
如果没出现重复字符串就继续遍历,每次更新res长度
如果出现了重复,那就把最左边的坐标移到前边重复数字的下一个,然后继续遍历,res一直是记录最大长度
与这种做法相比,第一种做法每次都会遍历很多重复的子串
*/
//所有字符都能转化为256以内的int值,这样就可以记录所有字符
int[] index = new int[256];
Arrays.fill(index,-1);
//记录 最左边坐标
int left = 0;
//记录结果
int res = 0;
for (int i = 0; i < s.length(); i++) {
//先检测有没有重复字符,如果,left更新,比较的方法是把哈希表中的记录和left比较,取大的。如果没有出现过,那么哈希表中的记录是-1,肯定是left大
//如果出现过,有两种情况,一种是记录没有left大,说明是和left 之前的一个重复了,这种情况没有影响 ,另一种是比left大,这样就会影响长度的判断,要更新left
left = Math.max(left,index[s.charAt(i)+1]);
//更新哈希表
index[s.charAt(i)] = i;
res = Math.max(res,i-left+1);
}
return res;
}
3. Longest Substring Without Repeating Characters寻找不重复的最大子串的更多相关文章
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem: Given a string, find the length of the longest substring without repeating characters. For ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- IEEE754标准浮点数表示与舍入
原文地址:https://blog.fanscore.cn/p/26/ 友情提示:本文排版不太好,但内容简单,请耐心观看,总会搞懂的. 1. 定点数 对于一个无符号二进制小数,例如101.111,如果 ...
- Django连接redis
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", &q ...
- Django的静态文件的配置
静态文件配置 STATIC_URL = '/static/' # 静态文件配置 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] # 暴露给 ...
- 对于Web开发最棒的22个Visual Studio Code插件
翻译 原文作者:James Quick 原文地址:https://scotch.io/bar-talk/22-best-visual-studio-code-extensions-for- ...
- redis分布式锁解决超卖问题
redis事务 redis事务介绍: 1. redis事务可以一次执行多个命令,本质是一组命令的集合. 2.一个事务中的所有命令都会序列化,按顺序串行化的执行而不会被其他命令插入 作用:一个队列 ...
- 九. Vuex详解
1. 理解Vuex 1.1 Vuex功能 官方解释 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用 集中式存储 管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方 ...
- PyQt(Python+Qt)学习随笔:Model/View中的枚举类 Qt.MatchFlag的取值及含义
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 枚举类 Qt.MatchFlag描述在模型中搜索项时可以使用的匹配类型,它可以在QStandardI ...
- PyQt(Python+Qt)学习随笔:model/view架构中的两个标准模型QStandardItemModel和QFileSystemModel
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.PyQt中的标准模型 PyQt和Qt提供了两个标准模型QStandardItemModel和QF ...
- centos 7系统,解决python3.x 安装后使用yum不能安装的问题(错误:urlgrabber-ext-down | KeyboardInterrupt)
1.在安装python3.xx版本后,通过yum去安装软件会出现问题,目前我遇到的有2种问题 比如显示:urlgrabber-ext-down Downloading packages: File & ...
- VirtualBox安装Centos出现E_FAIL (0x80004005)的解决方法
问题描述:UUID已经存在 Cannot register the hard disk 'F:\hadoop\VirtualBox-centos\centos6.4\centos6.4.vdi' {0 ...