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.

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
      int max=0;
      int pos[256];
      memset(pos,-1,sizeof(int)*256);
      int index=-1;
      for(int i=0;i<s.length();i++)
      {
          if(pos[s[i]]>index)
          {
              index=pos[s[i]];
          }
          if(i-index>max)
          {
              max=i-index;
          }
          pos[s[i]]=i;
      }
      return max;
    }
};

 

posted @ 2015-02-10 13:58  Vae永Silence  阅读(112)  评论(0编辑  收藏  举报