LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Characters(顺位题号是3)。给定一个字符串,找到最长无重复字符子字符串的长度。例如:
输入:“abcabcbb”
输出:3
说明:答案是“abc”,长度为3。
输入:“bbbbb”
输出:1
说明:答案是“b”,长度为1。
输入:“pwwkew”
输出:3
说明:答案是“wke”,长度为3。请注意,答案必须是子字符串,“pwke”是子序列而不是子字符串。
02 第一种解法
暴力解法,使用双指针和HashSet。
每次选取一个字符,从该字符开始往后遍历,存入HashSet中去,借助HashSet来判断是否重复出现,如果遇到重复字符,就结束循环,计算起始索引的长度,然后将HashSet清空,继续循环,再从第二个字符开始遍历,直到处理完所有字符。
此解法的时间复杂度是O(N^2),最坏情况下时间复杂度是O(N^3),因为HashSet的contains方法;空间复杂度是O(N)。
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<Character>();
int n = s.length(), left = 0, right = 0;
int result = 0;
for (int i=0; i<n; i++) {
left = i;
right = i;
while (right < n && !set.contains(s.charAt(right))) {
set.add(s.charAt(right));
right++;
}
result = Math.max(result, right-left);
set.clear();
}
return result;
}
03 第二种解法
针对第一种解法,我们还可以将HashSet换成数组,用数组来判断字符是否重复出现,其他思路一样。
public int lengthOfLongestSubstring2(String s) {
int[] set = new int[256];
int n = s.length(), left = 0, right = 0;
int result = 0;
for (int i=0; i<n; i++) {
left = i;
right = i;
while (right < n && ++set[s.charAt(right)] < 2) {
right++;
}
result = Math.max(result, right-left);
// 将数组元素值全部重置为0
Arrays.fill(set, 0);
}
return result;
}
04 第三种解法
滑动窗口算法。
此解法和上面的第一种解法有点类似,使用两个变量,一左一右,像窗口一样滑动,遇到重复值时,就将左边的一个字符从HashSet中移除,直到遍历完所有字符。
此解法的时间复杂度是O(N),最坏情况下时间复杂度是O(N^2),因为HashSet的contains方法;空间复杂度是O(N)。
public int lengthOfLongestSubstring3(String s) {
Set<Character> set = new HashSet<Character>();
int n = s.length(), left = 0, right = 0;
int result = 0;
while (left < n && right < n) {
if (!set.contains(s.charAt(right))) {
set.add(s.charAt(right));
right++;
result = Math.max(result, right-left);
} else {
set.remove(s.charAt(left));
left++;
}
}
return result;
}
05 第四种解法
使用索引值来判断,同时将HashSet换成256大小的数组。
public int lengthOfLongestSubstring4(String s) {
int[] set = new int[256];
int n = s.length(), i = 0, j = 0;
int result = 0;
while (i < n && j < n) {
i = Math.max(set[s.charAt(j)], i);
result = Math.max(result, j-i+1);
set[s.charAt(j)] = j+1;
j++;
}
return result;
}
06 小结
算法专题目前已连续日更超过六个月,算法题文章210+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,好看、留言、转发就是对我最大的回报和支持!
LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)的更多相关文章
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Swift]LeetCode3. 无重复字符的最长子串 | 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 - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- 最长无重复字符的子串 · Longest Substring Without Repeating Characters
[抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...
- 求一字符串最长不重复字符子串的长度【Java 版】
一. 前言 最近学习有点断断续续,整理的一些知识点要么不完整,要么完全没搞懂,不好拿上台面,还是先在草稿箱躺着吧.偶尔在浏览大牛博客http://coolshell.cn的时候,发现大牛业余时间也在做 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
随机推荐
- vue里的tab控件
如下图,v-model绑定的值,这个值在js里一旦改变,视图就会切换到相应的tab页,这意味着一定要先给tab页内容数据赋值,再改变这个tabsIndex的值 如下图,先赋值data列表数据,在更改t ...
- 扩增子图表解读6韦恩图:比较组间共有和特有OTU或分类单元
韦恩图 Venn Diagram Venn Diagram,也称韦恩图.维恩图.文氏图,用于显示元素集合重叠区域的图示. 韦图绘制工具 常用R语言的VennDiagram包绘制,输出PDF格式方便 ...
- ubuntu下Fiddler抓包
参考 https://www.cnblogs.com/jcli/p/4474332.html https://www.jianshu.com/p/4505c732e378 1. 你要有个Mono环境, ...
- ES6 作用域的问题
- 多目标跟踪笔记二:Efficient Algorithms for Finding the K Best Paths Through a Trellis
Abstract 本文提出了一种新的方法来寻找不相交k最优路径.最坏情况下计算复杂度为N3log(N).该方法比WVD算法(https://www.cnblogs.com/walker-lin/p/1 ...
- 浅谈 extern "C"
今天上课实在无聊,就看了看 extern "C" 的作用,看了以后对它有了一点点理解,在这里给大家分享一下(本菜鸡水平有限,如若有说得不对的地方,还望大家指出). extern 关 ...
- VS单元测试"未能加载文件或程序集,或它的某一个依赖项"
Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular ...
- STM32串口IAP实验笔记
STM32的IAP功能确实方便,以前对此如何实现有所了解,但是一直没去测试,这两天来练了下,可谓困难重重,搞了两天问题也一一解决,下面做些简要的笔记 IAP就是在线应用编程,方便程序升级,可以不用打开 ...
- 6.3.1 使用 pickle 模块读写二进制文件
Python 标准库 pickle 提供的 dump() 方法 用于将数据进行序列化并写入文件(dump() 方法的protocol 参数为True 时可以实现压缩的效果),而load() 用于读取二 ...
- Codeforces Round #411(Div. 2)——ABCDEF
30min水掉前面4T,30min尝试读懂EF题,60min划水 顺便D忘记取膜丢50分,距比赛结束10s时hack失败丢50分... 从2620掉分到2520,从rank227掉到rank354.. ...