[leetcode] 3. Longest Substring Without Repeating Characters

版权声明:by ruohua3kou https://blog.csdn.net/ruohua3kou/article/details/82748867

原题链接

求最长不连续子序列
利用map存储每一个字符上一次出现的位置,
遍历string,
比较 当前位置与上一次出现该字母的位置之差记录的最大值 的大小
修改最大值。

class Solution
{
public:
  map<char, int> maps;
  int lengthOfLongestSubstring(string s)
  {
    int i, Max = 0, pre = -1;
    for (i = 0; i < s.length(); i++)
      maps[s[i]] = -1;
    for (i = 0; i < s.length(); i++)
    {
      
      pre = max(pre, maps[s[i]]);
      Max = max(Max, i - pre);
      maps[s[i]] = i;
    }
    return Max;
  }
};

猜你喜欢

转载自blog.csdn.net/ruohua3kou/article/details/82748867