OJ无重复字符的最长子串(C++实现)

题目:无重复字符的最长子串

代码实现如下:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int maxlen = 0;
        string str("");
        if(s.size() == 1){
            return 1;
        }
        if(s.size() == 0){
            return 0;
        }
        int left = 0;
        int right = 0;
        while(1){
            auto tmp = str.find(s[right]);
            if(tmp != -1){
                //left从0开始
                left++;
                //更新right的值
                right = left;
                if(maxlen < str.size())
                    maxlen = str.size();
                str = "";
            }
            else{
                str += s[right];
                right++;
            }
            
            if(right == s.size()){
                if(maxlen < str.size())
                    maxlen = str.size();
                break;
            }
        }
        return maxlen;
    }
};

你可能感兴趣的:(OJ无重复字符的最长子串(C++实现))