LeetCode3——Longest Substring Without Repeating Characters

来源:互联网 发布:电脑测温软件 编辑:程序博客网 时间:2024/05/18 02:37

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 flag[256];
  memset(flag, 0, 256*sizeof(int));
  int i = 0;
  int len = 0;
  while (i < s.size()) {
  int k = i;
  while (flag[s[k]] == 0) {
  len++;
  flag[s[k]] = len;
  k++;
  if (k >= s.size()) goto OK;
 
  {
  if (max < len) {
  max = len;
  }
  i = i + flag[s[i]];
  len = 0;
  memset(flag, 0, 256*sizeof(int));
  }
  }
  OK:
  if (max < len) max = len;
  return max;
    }
};

0 0