LeetCode

无重复字符串

2019-01-08  本文已影响0人  hsinsDfy

思路:一个temp字符串,暂存当前字符串,速度不优,继续改
碰到重复就停下,temp为最长字符串,从下一个开始继续搜索,新temp为temp.find(s[i])+1到temp最后一位,当满足字符串长度大于当前最长字符串长度时,存字符串长度及字符串。

class Solution {
public:
 int lengthOfLongestSubstring(string s) {
        string result="",temp="";
        int max=0;
     int i=0;
     if(s!=" "){
         while(i<s.length()){
            if((temp.find(s[i]))==temp.npos){
                temp+=s[i];
                 i++;
                if(temp.length()>max)
                    max=temp.length();
              
            } 
            else {
                string tempp=temp;
                temp="";
                for(int j= tempp.find(s[i])+1;j<tempp.length();j++){
                    temp+=tempp[j];
}
            }
               
        }
     }
     else max=1;
        return max;
    }
};
上一篇 下一篇

猜你喜欢

热点阅读