这是悦乐书的第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),因为HashSetcontains方法;空间复杂度是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)的更多相关文章

  1. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

  2. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

  5. 最长无重复字符的子串 · Longest Substring Without Repeating Characters

    [抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...

  6. 求一字符串最长不重复字符子串的长度【Java 版】

    一. 前言 最近学习有点断断续续,整理的一些知识点要么不完整,要么完全没搞懂,不好拿上台面,还是先在草稿箱躺着吧.偶尔在浏览大牛博客http://coolshell.cn的时候,发现大牛业余时间也在做 ...

  7. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  9. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

随机推荐

  1. git怎么克隆远程仓库到本地仓库

    参考: https://blog.csdn.net/zhangzeshan/article/details/81564990 不知道为什么输入git的克隆地址就会提示密码错误 ,使用http地址就直接 ...

  2. .htaccess使用

    RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...

  3. 1 Web 知识基础

    一.什么是跨域访问举个栗子:在A网站中,我们希望使用Ajax来获得B网站中的特定内容.如果A网站与B网站不在同一个域中,那么就出现了跨域访问问题.你可以理解为两个域名之间不能跨过域名来发送请求或者请求 ...

  4. python之BeautifulSoup库

    1. BeautifulSoup库简介 和 lxml 一样,Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.lxml 只会局部遍历 ...

  5. P4047 [JSOI2010]部落划分(最小生成树)

    题目描述 聪聪研究发现,荒岛野人总是过着群居的生活,但是,并不是整个荒岛上的所有野人都属于同一个部落,野人们总是拉帮结派形成属于自己的部落,不同的部落之间则经常发生争斗.只是,这一切都成为谜团了——聪 ...

  6. Flask - 特殊装饰器 和 Flask工作结构模式(FBV, CBV)

    目录 Flask - 特殊装饰器 和 Flask工作结构模式 @app.errorhandler() @app.before_request @app.after_request FBV和CBV Fl ...

  7. TypeError: CleanWebpackPlugin is not a constructor

    在项目中引入clean-webpack-plugin打包后报错 new CleanWebpackPlugin(), ^ TypeError: CleanWebpackPlugin is not a c ...

  8. mybatis源码阅读-初始化六个工具(六)

    六个基本工具图集 图片来源:https://my.oschina.net/zudajun/blog/668596 ObjectFactory 类图 接口定义 public interface Obje ...

  9. 51nod——1432 独木桥

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 ...

  10. 洛谷 P1481 魔族密码

    P1481 魔族密码 题目描述 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之子:我呕……(杀死人的眼神)快说题目!否则……-_-### 花 ...