leetcode--Longest Substring Without Repeating Characters
2013-08-19 00:49
441 查看
1.题目描述
2.解法分析
要找出最长的字符不重复的字串,由不重复二字很容易让人想到hash表,所以我的思路很自然就到unordered_set上面了,对于字符串的题目,很明显需要遍历字符串,遍历过程中设置一个变量随时记录当前得到字符不重复的最长字符的长度,然后设置一个变量记录以当前字符为结束字符的最长字符不重复的子串的长度,这样一定遇到重复的话可以立马根据这个变量回搜。代码如下:
我的算法在最坏情况下的时间复杂度是不好描述,肯定是大于O(N)的,但是我觉得肯定是小于O(NlgN)的,回搜的代价不会太大,但是我总是在想是不是有其他的精妙的解法呢,结果在网上还真找了个,在discuss里面有个算法,乍一看没看懂:
[code]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. [/code] |
2.解法分析
要找出最长的字符不重复的字串,由不重复二字很容易让人想到hash表,所以我的思路很自然就到unordered_set上面了,对于字符串的题目,很明显需要遍历字符串,遍历过程中设置一个变量随时记录当前得到字符不重复的最长字符的长度,然后设置一个变量记录以当前字符为结束字符的最长字符不重复的子串的长度,这样一定遇到重复的话可以立马根据这个变量回搜。代码如下:
[code]class Solution { public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function if(s.length()<=1)return s.length(); unordered_set<char> myHash; int LongestLen=0; int curLen=0; for(int i=0;i<s.length();++i) { if(myHash.count(s[i])==1) { for(int j=i-curLen;j<i;++j) { if(s[j]==s[i]) { curLen = i-j;break; } else { myHash.erase(s[j]); } } } else { curLen++; if(LongestLen<curLen)LongestLen=curLen; myHash.insert(s[i]); } } return LongestLen; } }; [/code] |
我的算法在最坏情况下的时间复杂度是不好描述,肯定是大于O(N)的,但是我觉得肯定是小于O(NlgN)的,回搜的代价不会太大,但是我总是在想是不是有其他的精妙的解法呢,结果在网上还真找了个,在discuss里面有个算法,乍一看没看懂:
[code]class Solution { public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> pos(256, -1); int begin = 0, ret = 0; for (int i = 0; i < s.size(); ++i) { begin = max(begin, pos[s[i]] + 1); pos[s[i]] = i; ret = max(ret, i - begin + 1); } return ret; } }; [/code] |
相关文章推荐
- Longest Substring without repeating characters--LeetCode
- [leetcode] #3 Longest Substring Without Repeating Characters
- LeetCode------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
- Leetcode3 : 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
- 3 leetcode - Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- [LeetCode]3 Longest Substring Without Repeating Characters (C++,Python实现)
- [LeetCode]3 Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- leetcode解题方案--003--LongestSubstringWithoutRepeatingCharacters
- [leetcode-3] Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- LeetCode(3) - Longest Substring Without Repeating Characters