【leetcode】第三题

给定一个字符串,找出不含有重复字符的最长子串的长度。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int slength=s.length();
        int maxlength=0;
        int currentlength=1;
        int i=0;
        int j=1;
        int same=0;
        int nowpoint=0;
        int lastlength=1;
        if(slength==1){
             maxlength=1;
        }else{
            while(j<slength){
           
            for(nowpoint=i;nowpoint>=i-lastlength+1;--nowpoint){
                if(s[j]==s[nowpoint]){
                    same=1;
                    break;
                }
            }
            if(same){
                 i=nowpoint+1;
                 j=i+1;
                 maxlength<lastlength?maxlength=lastlength:maxlength;
                 currentlength=1;
                 lastlength=1;
            }else{
                i++;j++;
                lastlength=++currentlength;
                maxlength<lastlength?maxlength=lastlength:maxlength;
            }
            same=0;
        }
        }
        return  maxlength; 
    };
    
    
};

猜你喜欢

转载自blog.csdn.net/transformed/article/details/83622913