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;

    }

};

 

你可能感兴趣的:(substring)