[leetcode] 3. Longest Substring Without Repeating Characters

it2025-11-16  2

原题链接

求最长不连续子序列 利用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; } };

转载于:https://www.cnblogs.com/ruoh3kou/p/9893448.html

最新回复(0)