当前位置:网站首页>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
};
边栏推荐
- What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
- Li Kou daily question 1 (2)
- [embedded] cortex m4f DSP Library
- 超高效!Swagger-Yapi的秘密
- 生成器参数传入参数
- Alibaba cloud server mining virus solution (practiced)
- Tdengine biweekly selection of community issues | phase III
- C语言双指针——经典题型
- Marathon envs project environment configuration (strengthen learning and imitate reference actions)
- [sword finger offer] serialized binary tree
猜你喜欢
C language double pointer -- classic question type
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
Simple use of promise in uniapp
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
View computer devices in LAN
【ROS】usb_ Cam camera calibration
Delay initialization and sealing classes
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
Navicat premium create MySQL create stored procedure
Deep analysis of C language data storage in memory
随机推荐
Double pointeur en langage C - - modèle classique
Image, CV2 read the conversion and size resize change of numpy array of pictures
sublime text的编写程序时的Tab和空格缩进问题
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
MySQL uninstallation and installation methods
Export IEEE document format using latex
Excellent software testers have these abilities
Leetcode刷题题解2.1.1
LeetCode:214. 最短回文串
Promise 在uniapp的简单使用
Generator parameters incoming parameters
Bitwise logical operator
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
LeetCode:41. 缺失的第一个正数
Visual implementation and inspection of visdom
The harm of game unpacking and the importance of resource encryption
如何正确截取字符串(例:应用报错信息截取入库操作)
Mongodb installation and basic operation
[sword finger offer] serialized binary tree
pytorch训练好的模型在加载和保存过程中的问题