leetcode-Longest Substring Without Repeating Characters


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.


class Solution {
public:
    int lengthOfLongestSubstring(string s)
    {
       int hash[256];
       for(int i=0;i<256;i++)
            hash[i] = -1;
       int len = s.length();
       int i;
       int max_len = 0;
       int index = -1;
       for(i=0;i<len;++i)
       {
          if(hash[s[i]]>index)
            index = hash[s[i]];
          if(i-index>max_len)
            max_len = i - index;
            
          hash[s[i]] = i;
          
       }
       return max_len;
    }
};


说明:

1. hash[256]={-1}, 这个只能是hash[0]=-1, 其它的值不确定,虽然默认为0

2. memset(hash,1,256);这个是前256个字符是1
 

每天早上叫醒你的不是闹钟,而是心中的梦~
原文地址:https://www.cnblogs.com/vintion/p/4116826.html