leetcode系列(31)Longest Substring Without Repeating Characters 最长不重复子串
来源:互联网 发布:手机淘宝加入购物车 编辑:程序博客网 时间:2024/06/14 06:35
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
这个题目用O(n^2)时间复杂度,空间复杂度为O(1)的解法和时间复杂度为O(n)空间复杂度为O(n)的解法,下面的C++代码和Python代码分别
都给出两种复杂度的解法。
C++代码
class Solution { // Times O(n^2) Space O(1)public: int lengthOfLongestSubstring(string s) { int max = 0; int cur_max = 0; int start = 0; for (int end = 0; end < s.length(); ++end) { int idx = _is_exist(s, start, end); if (idx != end) { start = idx + 1; } cur_max = end - start + 1; if (cur_max > max) { max = cur_max; } } return max; } private: int _is_exist(const string& s, int start, int end) { for (int i = start; i < end; ++i) { if (s[i] == s[end]) { return i; } } return end; } };class Solution { // Times O(n), Space O(n)public: int lengthOfLongestSubstring(string s) { int max = 0; int cur_max = 0; int start = 0; for (int end = 0; end < s.length(); ++end) { char item = s[end]; if (umap_.find(item) == umap_.end()) { umap_[item] = end; } int idx = umap_[item]; if (idx >= start && idx < end) { start = idx + 1; } umap_[item] = end; // alway to update umap_ cur_max = end - start + 1; if (cur_max > max) { max = cur_max; } } return max; } private: std::unordered_map<char, int> umap_;};
Python代码
class Solution(object): def lengthOfLongestSubstring(self, s): """ Times O(n^2) Space O(1) :type s: str :rtype: int """ cur_max = 0 ret = 0 start = 0 end = 0 for end in range(0, len(s)): index = s.find(s[end], start, end) if index != -1: start = index + 1 cur_max = end - start + 1 if cur_max > ret: ret = cur_max return retclass Solution(object): def lengthOfLongestSubstring(self, s): """ Time O(n) Space O(n) :type s: str :rtype: int """ cur_max = 0 ret = 0 start = 0 end = 0 umap = dict() for end in range(0, len(s)): item = s[end] if item not in umap: umap[item] = end idx = umap[item]; if idx >= start and idx < end: start = idx + 1 umap[item] = end cur_max = end - start + 1 if cur_max > ret: ret = cur_max return ret
0 0
- leetcode系列(31)Longest Substring Without Repeating Characters 最长不重复子串
- LeetCode | Longest Substring Without Repeating Characters(最长连续不重复子串)
- [C++]LeetCode 3: 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 3. Longest Substring Without Repeating Characters 最长不重复子串和重复子串
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串查找
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串
- leetcode-Longest Substring Without Repeating Characters 最长不重复子串
- Longest Substring Without Repeating Characters 最长不重复子串 @LeetCode
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- LeetCode OJ:Longest Substring Without Repeating Characters(最长不重复子串)
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- leetcode Longest Substring Without Repeating Characters 最长不重复子串
- Leetcode 3. Longest Substring Without Repeating Characters(最长不重复子串)
- leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)
- Longest Substring Without Repeating Characters (最长不重复子串)
- windows系统下搭建svn服务器
- worldwind参考博客
- unity5+Vuforia SDK5实现高通AR在unity下的环境构建
- Linux 菜鸟笔记(一)
- 高光BRDF化简公式
- leetcode系列(31)Longest Substring Without Repeating Characters 最长不重复子串
- lucene4.4与IKAnalyzer的冲突
- 音乐播放器app开源代码
- Java的GC机制详解
- 《Cocos2d-x实战(卷Ⅰ):C++开发》目录 (仅供参考)
- SOCKET访问基本模型
- oracle触发器
- Tomcat+Servlet保存Cookie到浏览器
- Pay Your Debts