找一个串的最长的字母不重复的子串。

感觉和最小摘要差不多,两个指针搞搞就好。

start,end表示子串开始和结束位置

如果end放进去没有重复,那么end++

如果放入有重复,那么start++,一直到end放入没有重复。

然后就这样下去,找到最长的一个。

 

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if (s == "") return 0;
        int start = 0;
        int end = start + 1;
        int len = s.length();
        bool charset[500] = {false};
        int ans = 1;
        charset[s[0]] = true; //记得要初始化啊!!!,就这个检查半天
        while(start < end && end < len){
            if(charset[s[end]] == false){
                //cout << s[end] << " " << charset[s[end]]<<endl;

                charset[s[end]] = true;
                end++;
                //cout << start << " " << end <<endl;
                if (end - start > ans) ans = end - start;
            }else{
                while(charset[s[end]] == true){
                    charset[s[start]] = false;
                    start++;
                }
                charset[s[end]] = true;
       
                end ++;
               if (end - start > ans) ans = end - start;
             
            }

        }
        
        return ans;
    }

};

 

相关文章:

  • 2021-08-03
  • 2022-01-06
  • 2022-01-20
  • 2021-05-25
猜你喜欢
  • 2021-09-13
  • 2021-09-11
  • 2021-08-28
  • 2021-10-04
  • 2021-08-01
  • 2022-02-13
相关资源
相似解决方案