最长不重复子串
从头开始遍历字符串,对每一个遍历过的字符判断是否被标记过,若标记过,则更新左边界le=vis[temp]+1,同时更新当前不重复子串的长度;否则用数组下标进行标记,不断右移右边界ri,更新最大长度
class Solution { public: int lengthOfLongestSubstring(string s) { int le = 0, ri = 0, maxlen = 0, ans = 0; int len = s.length(); int vis[256]; for (int i = 0; i < 256; i++) vis[i] = -1; while(ri<len) { int temp = (char)s[ri]; if (vis[temp] >=le) { le = vis[temp] + 1; maxlen = ri - le; } vis[temp] = ri; ri++; maxlen++; ans = max(ans, maxlen); } return ans; } };
等风起的那一天,我已准备好一切