给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

 

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         //滑动窗口:右边界由遍历确定,若字符没有出现过,那么右边界加一,若字符之前出现过,则判断是否在滑动窗口出现过,若出现过,则left移动,若没有则右边界加一
 5       
 6         int res=0, left=-1;
 7         unordered_map<int,int> map;
 8         
 9         for(int i=0;i<s.length();i++){
10             if(map.count(s[i])&& map[s[i]]>left)
11             {
12                 left = map[s[i]];
13             }
14             map[s[i]] = i;           
15             res = max(res,i-left);
16             
17         }
18             
19       return res; 
20        
21     }
22 };

 

posted on 2019-01-18 20:42  柠檬檬檬  阅读(86)  评论(0编辑  收藏  举报