当前位置:网站首页>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
};
边栏推荐
- 704 binary search
- [brush questions] top101 must be brushed in the interview of niuke.com
- How to conduct interface test? What are the precautions? Nanny level interpretation
- Is it safe to open an account in Zheshang futures?
- Mobile phones and computers on the same LAN access each other, IIS settings
- The harm of game unpacking and the importance of resource encryption
- JVM performance tuning and practical basic theory - Part 1
- 深度剖析C语言数据在内存中的存储
- Sort according to a number in a string in a column of CSV file
- pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
猜你喜欢
Promise 在uniapp的简单使用
Excellent software testers have these abilities
704 binary search
Navicat Premium 创建MySql 创建存储过程
PC easy to use essential software (used)
C language double pointer -- classic question type
Deep anatomy of C language -- C language keywords
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
Detailed explanation of heap sorting
堆排序详解
随机推荐
TP-LINK 企业路由器 PPTP 配置
Deep analysis of C language data storage in memory
MySQL learning record 07 index (simple understanding)
Shift Operators
Roguelike游戏成破解重灾区,如何破局?
目标检测——Pytorch 利用mobilenet系列(v1,v2,v3)搭建yolov4目标检测平台
China vanadium battery Market Research and future prospects report (2022 Edition)
LeetCode:26. 删除有序数组中的重复项
Variable length parameter
Computer cleaning, deleted system files
Bottom up - physical layer
【ROS】usb_ Cam camera calibration
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
MySQL learning record 10getting started with JDBC
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
【ROS】usb_cam相机标定
Revit secondary development Hof method calls transaction
【Nvidia开发板】常见问题集 (不定时更新)
电脑清理,删除的系统文件
egg. JS directory structure