leetcode 3: 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.

mistakes I made:1. forget record start point. I reset longest to 0 after encountering a repeated char, which is wrong. It should start from the next char following the first char of two repeated chars.

[cpp]

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int locs[256];//保存字符上一次出现的位置
        memset(locs, -1, sizeof(locs));

        int idx = -1, max = 0;//idx为当前子串的开始位置-1
        for (int i = 0; i < s.size(); i++)
        {
            if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
            {
                idx = locs[s[i]];
            }

            if (i - idx > max)
            {
                max = i - idx;
            }

            locs[s[i]] = i;
        }
        return max;
    }
};


[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.     int lengthOfLongestSubstring(string s) {  
  4.         bool d[256] = { false };  
  5.         int max_len = 0;  
  6.         int start = 0;  
  7.         for(int i=0; i<s.size(); i++) {  
  8.             char c = s[i];  
  9.             if(!d[c]) {  
  10.                 d[c] = true;  
  11.                 max_len = max(max_len, i-start+1);   
  12.             } else {  
  13.                 while(s[start] != c) {  
  14.                     d[s[start]] = false;  
  15.                     ++start;  
  16.                 }  
  17.                 ++start;  
  18.             }  
  19.         }  
  20.         return max_len;  
  21.     }  
  22. };  



[java]  view plain copy
  1. class Solution {  
  2. public:  
  3.     int lengthOfLongestSubstring(string s) {  
  4.         // Start typing your C/C++ solution below  
  5.         // DO NOT write int main() function  
  6.         if( s.size() == 0return 0;  
  7.           
  8.         #define MAX_LEN 256  
  9.         char flag[MAX_LEN];  
  10.         reset(flag, MAX_LEN);  
  11.           
  12.         int max = 0;  
  13.         int longest = 0;  
  14.         int start = 0;  
  15.           
  16.         for(int i=0; i<s.size(); i++) {  
  17.             if( flag[ s[i] ] == -1 ){  
  18.                 longest++;  
  19.             } else {  
  20.                 max = max < longest ? longest : max;  
  21.                   
  22.                 for(int j=start; j<i; j++){  
  23.                     flag[ s[j] ] = -1;  
  24.                     if( s[j] == s[i] ) {  
  25.                         start = j + 1;  
  26.                         break;  
  27.                     }    
  28.                 }  
  29.                 longest = i - start + 1;  
  30.                   
  31.             }  
  32.               
  33.             flag[ s[i] ] = 1;  
  34.               
  35.         }  
  36.         max = max < longest ? longest : max;  
  37.         return max;  
  38.     }  
  39.       
  40. private:  
  41.     void reset(char a[], int n){  
  42.         for(int i=0; i<n; i++) {  
  43.             a[i] = -1;  
  44.         }  
  45.     }   
  46.   
  47. };  


 

[java]  view plain copy
  1. public class Solution {  
  2.     public int lengthOfLongestSubstring(String s) {  
  3.         // Start typing your Java solution below  
  4.         // DO NOT write main() function  
  5.         int max = Integer.MIN_VALUE;  
  6.         int start = 0;  
  7.         HashMap<Character,Integer> map = new HashMap<Character, Integer>();  
  8.           
  9.         for(int i=0; i<s.length(); i++) {  
  10.             char c = s.charAt(i);  
  11.             if( map.containsKey(c)){  
  12.                 for(int j=start;j<i;j++) {  
  13.                     if(s.charAt(j)==c) break;  
  14.                     map.remove(s.charAt(j));  
  15.                 }  
  16.                 start = map.get(c)+1;  
  17.                 map.put(c,i);  
  18.             } else {  
  19.                 map.put(c,i);  
  20.                 if( i-start+1 > max ) max = i-start+1;  
  21.             }  
  22.         }  
  23.         return max;  
  24.     }  
  25. }  

c++: 
[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.   
  4.   
  5.     int lengthOfLongestSubstring(string s) {  
  6.         // Start typing your C/C++ solution below  
  7.         // DO NOT write int main() function  
  8.         unordered_set<char> myset;  
  9.         int sz = s.size();  
  10.           
  11.         int cur = 0;  
  12.         int max = 0;  
  13.         int start = 0;  
  14.           
  15.         for(int i=0; i<sz; i++) {  
  16.             char c = s[i];  
  17.               
  18.             if( myset.find(c) == myset.end() ) {  
  19.                 myset.insert(c);  
  20.                 cur++;  
  21.                 max = max>cur ? max : cur;  
  22.             } else {  
  23.                 int j=start;  
  24.                 while( s[j]!=c) {  
  25.                     myset.erase( s[j] );  
  26.                     ++j;  
  27.                 }  
  28.                 start = j+1;  
  29.                 cur = i-start+1;  
  30.             }  
  31.         }  
  32.         return max;  
  33.     }  
  34. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值