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
原创粉丝点击