【LeetCode】3. 无重复字符的最长子串(Longest Substring Without Repeating Characters,24.2%,中等)
2018-09-03 16:50
609 查看
版权声明:原创不易,转载须经作者同意 https://blog.csdn.net/zhichaosong/article/details/82351096
一、题目描述
给定一个字符串,找出不含有重复字符的最长子串的长度。
示例1:
输入:”abcabcbb”
输出:3
解释:无重复字符的最长子串是 “abc”,其长度为 3。
示例2:
输入:”bbbbb”
输出:1
解释:无重复字符的最长子串是 “b”,其长度为 1。
示例3:
输入:”pwwkew”
输出:3
解释:无重复字符的最长子串是 “wke”,其长度为 3。
请注意,答案必须是一个子串,”pwke” 是一个子序列 而不是子串。
二、思路分析
解决问题分两步:
① 搜索所有课程的子串
② 判断子串是否有重复字符
第①步可以暴力搜索,也可以用滑动窗口提高效率
第②步可以逐个比对字符,也可以用HashSet提高效率
三、解决方案
方法一:暴力+逐个字符比对法
public int lengthOfLongestSubstring(String s) { int max = 0; int length = s.length(); if (length > 0) { for (int i = 0; i < length; i++) { while ((i + max + 1) <= length && judgeStringRepeat(s.substring(i, i + max + 1))) { max++; } } } return max; } public boolean judgeStringRepeat(String s) { boolean res = true; int length = s.length(); for (int i = 0; i < length; i++) { for(int j=i+1; j<length; j++){ if (s.charAt(i) == s.charAt(j)) { res = false; break; } } } return res; }
- 时间复杂度:O(n3)O(n3),
- 空间复杂度:O(n)O(n),
方法二:滑动窗口+HashSet法
滑动窗口是数组/字符串问题中常用的抽象概念。 窗口通常是在数组/字符串中由开始和结束索引定义的一系列元素的集合,即 [i, j)[i,j)(左闭,右开)。而滑动窗口是可以将两个边界向某一方向“滑动”的窗口。例如,我们将 [i, j)[i,j) 向右滑动 11 个元素,则它将变为 [i+1, j+1)[i+1,j+1)(左闭,右开)。
public class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(); Set<Character> set = new HashSet<>(); int ans = 0, i = 0, j = 0; while (i < n && j < n) { // try to extend the range [i, j] if (!set.contains(s.charAt(j))){ set.add(s.charAt(j++)); ans = Math.max(ans, j - i); } else { set.remove(s.charAt(i++)); } } return ans; } }
- 时间复杂度:O(n)O(n)
- 空间复杂度:O(min(m,n))O(min(m,n)),滑动窗口法需要 O(k)O(k) 的空间,其中 kk 表示 Set 的大小。而Set的大小取决于字符串 nn 的大小以及字符集/字母 mm 的大小。
相关文章推荐
- LeetCode题库解答与分析——#3.无重复字符的最长子串LongestSubstringWithoutRepeatingCharacters
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- 3.无重复字符的最长子串(Longest Substring Without Repeating Characters)
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- [LeetCode] Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- leetcode-java.T003_LongestSubstringWithoutRepeatingCharacters 给定一个字符串,找字符中的最大非重复子串
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串查找
- (python3)最长无重复字符子串Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- 寻找最长无重复字符子串 Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters (返回最长无字符重复的子串长度)
- 字符的最长无重复子串长度Longest Substring Without Repeating Characters
- leetcode-Longest Substring Without Repeating Characters 最长不重复子串
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串