当前位置:网站首页>LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
2022-07-06 08:44:00 【Bertil】
请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
示例 1:
输入: "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:
输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:
输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
提示:
- s.length <= 40000
注意:本题与主站 3 题相同:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
解题思路
1.首先定义左指针,新建一个map来存放每个字符和它的索引值
2.然后遍历右指针,若字符已出现过,则左指针右移
3.最后根据左右指针的位置来不断取不含重复字符的子字符串的最大值
代码
/** * @param {string} s * @return {number} */
var lengthOfLongestSubstring = function(s) {
let l = 0
let res = 0
const map = new Map()
for(let r = 0; r < s.length; r++) {
if(map.has(s[r]) && map.get(s[r]) >= l) {
l = map.get(s[r]) + 1
}
res = Math.max(res, r - l + 1)
map.set(s[r],r)
}
return res
};
边栏推荐
- JS pure function
- 【ROS】usb_cam相机标定
- 深度剖析C语言数据在内存中的存储
- Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
- Colorlog combined with logging to print colored logs
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- C語言雙指針——經典題型
- LeetCode:26. 删除有序数组中的重复项
- 如何有效地进行自动化测试?
- Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
猜你喜欢
Sublime text using ctrl+b to run another program without closing other runs
被破解毁掉的国产游戏之光
LeetCode:236. 二叉树的最近公共祖先
win10系统中的截图,win+prtSc保存位置
Deep anatomy of C language -- C language keywords
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
UnsupportedOperationException异常
Precise query of tree tree
View computer devices in LAN
Problems in loading and saving pytorch trained models
随机推荐
Precise query of tree tree
Light of domestic games destroyed by cracking
LeetCode:剑指 Offer 04. 二维数组中的查找
TP-LINK 企业路由器 PPTP 配置
The mysqlbinlog command uses
MySQL learning record 07 index (simple understanding)
MySQL learning records 12jdbc operation transactions
Unsupported operation exception
704 binary search
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
Verrouillage [MySQL]
移位运算符
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
POI add write excel file
Cisp-pte practice explanation
Revit secondary development Hof method calls transaction
[MySQL] log
What are the common processes of software stress testing? Professional software test reports issued by companies to share
LeetCode:剑指 Offer 03. 数组中重复的数字
JS pure function