每日一算-3. 无重复字符的最长子串
2019-04-23 15:00
211 查看
每日一算-3. 无重复字符的最长子串
题目描述
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
解题思路
使用hash表,记录每个字符的出现的位置,一个start记录不重复字符串的起始位置,一次循环过程中,当前字符如果在hash表中存在,并且出现的位置>=start,则出现重复,更新start到重复字符坐标的下一个位置
代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(s.empty()) return 0;
if(s.size() == 1) return 1;
unordered_map<char,int> hash;
int start = 0;
int len = 0;
for(int i =0 ;i < s.size();i++) {
char cur = s[i];
if(hash.find(cur) != hash.end() && hash[cur] >= start) {
start = hash[cur] + 1;
} else {
len = i - start + 1 > len?i-start + 1:len;
}
hash[cur] = i;
}
return len;
}
};
相关文章推荐
- LeetCode 3. 无重复字符的最长子串
- Leetcode(C++)——3. 无重复字符的最长子串
- Leetcode——3.无重复字符的最长子串
- leetcode 3. 无重复字符的最长子串
- Leetcode 3. 无重复字符的最长子串
- leetcode之路 3. 无重复字符的最长子串
- LeetCode3. 无重复字符的最长子串(python)
- 【LeetCode】3.无重复字符的最长子串 结题报告 (C++)
- [Leetcode] 3.无重复字符的最长子串
- LeetCode题解(python)-3.无重复字符的最长子串
- LeetCode题目C++实现:3. 无重复字符的最长子串
- 3. 无重复字符的最长子串
- 【中级算法】3. 无重复字符的最长子串
- 【Leetcode一刷】3.无重复字符的最长子串
- Leetcode每日一道 -- 无重复字符的最长子串
- go语言LeeCode刷题记:3. 无重复字符的最长子串
- 3. 无重复字符的最长子串(O(N))
- leetcode习题集——3. 无重复字符的最长子串
- 【leetcode】3. 无重复字符的最长子串
- Leetcode刷题35-3.无重复字符的最长子串(C++)