当前位置:编程学习 > C/C++ >>

Leetcode: 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.
 
 
int lengthOfLongestSubstring(string s) {  
        // Start typing your C/C++ solution below  
        // DO NOT write int main() function  
        int longest = 0;  
        int slen = s.length();  
        if(slen < 0) return 0;  
        if(slen == 1) return 1;  
        int pre = 1;  
        for(int i = 1; i < slen; ++i)  
        {  
            char tmp = s[i];  
            int j = i-1;  
            int count = 1;  
            while((j>=0) && (tmp != s[j]))  
            {j--;count++;}  
            if(count >= pre+1) count = pre+1;  
            if(count > longest) longest = count;  
            pre = count;  
        }  
        return longest;  
    }  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,