您的位置:首页 > 博客中心 > 互联网 >

Leetcode(3) ;无重复字符的最长子串

时间:2022-05-07 06:41

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

 

 1 class Solution {
 2 public:
 3     int lengthOfLongestSubstring(string s) {
 4         //滑动窗口:右边界由遍历确定,若字符没有出现过,那么右边界加一,若字符之前出现过,则判断是否在滑动窗口出现过,若出现过,则left移动,若没有则右边界加一
 5       
 6         int res=0, left=-1;
 7         unordered_map map;
 8         
 9         for(int i=0;ileft)
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 };

 

本类排行

今日推荐

热门手游