当前位置:网站首页>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
};
边栏推荐
- pytorch查看张量占用内存大小
- Promise 在uniapp的简单使用
- Roguelike game into crack the hardest hit areas, how to break the bureau?
- Deep anatomy of C language -- C language keywords
- LeetCode:162. 寻找峰值
- Compétences en mémoire des graphiques UML
- Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
- C語言雙指針——經典題型
- UnsupportedOperationException异常
- [embedded] print log using JLINK RTT
猜你喜欢
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
多元聚类分析
数学建模2004B题(输电问题)
LeetCode:498. 对角线遍历
MySQL uninstallation and installation methods
C language double pointer -- classic question type
Generator parameters incoming parameters
Promise 在uniapp的简单使用
Deep analysis of C language pointer
随机推荐
LeetCode:236. 二叉树的最近公共祖先
R language uses the principal function of psych package to perform principal component analysis on the specified data set. PCA performs data dimensionality reduction (input as correlation matrix), cus
[NVIDIA development board] FAQ (updated from time to time)
自动化测试框架有什么作用?上海专业第三方软件测试公司安利
随手记01
Nacos 的安装与服务的注册
C语言深度解剖——C语言关键字
View computer devices in LAN
查看局域网中电脑设备
The harm of game unpacking and the importance of resource encryption
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
Shift Operators
Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
MySQL uninstallation and installation methods
LeetCode:26. 删除有序数组中的重复项
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
Tcp/ip protocol
Navicat Premium 创建MySql 创建存储过程
同一局域网的手机和电脑相互访问,IIS设置