首页>itarticle>leetcode longest substring without repeating characters
leetcode longest substring without repeating characters
admin11月 11, 20200
文章目录
tips
求没有重复字符的最长子串
memset(who, value, length)
left记录子串的最左边界,如果当前字符在left右面出现过,最更新left
time complexcity: O(N)
space complexcity: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class { public: intlengthOfLongestSubstring(string s){ int left = 0, ans = 0; int last[255]; memset(last, -1, sizeof(last)); for(int i = 0; i < s.size(); ++i) { if(last[s[i]] >= left) { left = last[s[i]] + 1; } last[s[i]] = i; ans = max(ans, i - left + 1); } return ans; } };
近期评论