3. Longest Substring Without Repeating Characters

xiaoxiao2021-02-28  60

题目:

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

思路:

本题思路比较清晰,首先建立map容器,遍历字符串,存储字符串当前字符与当前索引,维护一个变量max,一旦发现当前字符在前面出现过,就更新max,count清0,map清空,从前面出现的字符的下一个位置继续遍历,直到结束,最后再更新max,并返回输出

代码:

class Solution { public: int lengthOfLongestSubstring(string s) { unordered_map<char,int> mp; int count = 0,max = 0; for(int i=0;i<s.size();i++) { if(mp.find(s[i])==mp.end()) { count++; mp[s[i]] = i; } else { max = max>count?max:count; count = 0; i = mp.find(s[i])->second; mp.clear(); } } max = max>count?max:count; return max; } };

转载请注明原文地址: https://www.6miu.com/read-72723.html

最新回复(0)