LeetCode--3.Longest Substring Without Repeating Characters(无重复字符的最长子串)
2019-03-06 19:56
309 查看
题目描述
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
思路
1.暴力法
从字符串的每一个字符开始,直至遇到重复字符
时间复杂度O(n^2)
class Solution { public: int lengthOfLongestSubstring(string s) { int max=0; for(int i=0;i<s.length();++i) { int t=allUni(s,i,s.length()); if(max<t) max=t; } return max; } int allUni(string s,int start,int end) { set<char> a; int length=0; for(int i=start;i<end;++i) {//判断有没有重复,并计数 if(a.find(s[i])==a.end()) { ++length; a.insert(s[i]); } else break; } return length; } };
2.滑动窗口
时间复杂度O(n^2)
class Solution { public: int lengthOfLongestSubstring(string s) { set<char> a; int l=0,r=0;//滑动窗口,左右端 int n=s.length(); int max=0; while(l<n&&r<n) { if(a.find(s[r])!=a.end()) {//若存在,则去掉左端字母 a.erase(s[l++]); } else {//若找不到则右端++ a.insert(s[r++]); if(max<r-l) max=r-l; } } return max; } };
时间复杂度:O(n),索引 j 将会迭代 n 次。
空间复杂度(HashMap):O(min(m, n)),与之前的方法相同。
空间复杂度(Table):O(m),m 是字符集的大小。
思路是,用hashmap保存出现字符的位置,若遇到相同字符,则将滑动窗口的左端置为上一个出现的这个字符的下一个字符
class Solution { public: int lengthOfLongestSubstring(string s) { int max=0; map<char,int> map;//存放当前子串字符的出现位置 for(int i=0,j=0;j<s.length();++j) { if(map.find(s[j])!=map.end()) {//若找到了,则将i置为与map中存放该相同字符的位置的下一个字符 if(i<map[s[j]]+1) //若i比这个位置的下一个位置大,则不改变(即说明已经跳过了这个字符) i=map[s[j]]+1; } //保存位置(若进入过if,则修改位置) map[s[j]]=j; if(max<j-i+1) max=j-i+1; } return max; } };
相关文章推荐
- LeetCode-3 无重复字符的最长子串 Longest Substring Without Repeating Characters
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- LeetCode题库解答与分析——#3.无重复字符的最长子串LongestSubstringWithoutRepeatingCharacters
- [LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- LeetCode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
- 【LeetCode】3. 无重复字符的最长子串(Longest Substring Without Repeating Characters,24.2%,中等)
- LeetCode 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最长无重复子串
- LeetCode 3. Longest Substring Without Repeating Characters(最长不重复连续子串)
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串
- (python3)最长无重复字符子串Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters(最长不重复子串)
- [LeetCode] Longest substring without repeating characters 最长无重复子串
- 字符的最长无重复子串长度Longest Substring Without Repeating Characters
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode | Longest Substring Without Repeating Characters(最长连续不重复子串)