当前位置:网站首页>Leetcode: Sword finger offer 48 The longest substring without repeated characters
Leetcode: Sword finger offer 48 The longest substring without repeated characters
2022-07-06 08:51:00 【Bertil】
Please find the longest substring in the string that does not contain duplicate characters , Calculate the length of the longest substring .
Example 1:
Input : "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its length is 3.
Example 2:
Input : "bbbbb"
Output : 1
explain : Because the longest substring without repeating characters is "b", So its length is 1.
Example 3:
Input : "pwwkew"
Output : 3
explain : Because the longest substring without repeating characters is "wke", So its length is 3.
Please note that , Your answer must be Substring The length of ,"pwke" Is a subsequence , Not substring .
Tips :
- s.length <= 40000
Be careful : This topic and the main station 3 The question is the same :https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
Their thinking
1. First define the left pointer , Create a new one map To store each character and its index value
2. Then traverse the right pointer , If the character has appeared , Then the left pointer moves to the right
3. Finally, according to the position of the left and right pointers, the maximum value of the substring without repeated characters is constantly taken
Code
/** * @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
};
边栏推荐
- 有效提高软件产品质量,就找第三方软件测评机构
- gcc动态库fPIC和fpic编译选项差异介绍
- Pytorch view tensor memory size
- After reading the programmer's story, I can't help covering my chest...
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- Light of domestic games destroyed by cracking
- Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
- swagger设置字段required必填
- Alibaba cloud server mining virus solution (practiced)
- UML圖記憶技巧
猜你喜欢

Image,cv2读取图片的numpy数组的转换和尺寸resize变化

C language double pointer -- classic question type

MongoDB 的安装和基本操作

优秀的软件测试人员,都具备这些能力

Generator parameters incoming parameters

同一局域网的手机和电脑相互访问,IIS设置

vb.net 随窗口改变,缩放控件大小以及保持相对位置

Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform

Deep analysis of C language data storage in memory

Marathon envs project environment configuration (strengthen learning and imitate reference actions)
随机推荐
C语言深度解剖——C语言关键字
优秀的软件测试人员,都具备这些能力
ROS compilation calls the third-party dynamic library (xxx.so)
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
swagger设置字段required必填
Revit 二次开发 HOF 方式调用transaction
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
MYSQL卸载方法与安装方法
[MySQL] multi table query
如何进行接口测试测?有哪些注意事项?保姆级解读
UnsupportedOperationException异常
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
Tcp/ip protocol
LeetCode:836. 矩形重叠
Generator parameters incoming parameters
LeetCode:394. 字符串解码
Philosophical enlightenment from single point to distributed
How to conduct interface test? What are the precautions? Nanny level interpretation