当前位置:网站首页>Leetcode question brushing (question 3) - the longest substring without repeated characters
Leetcode question brushing (question 3) - the longest substring without repeated characters
2022-06-24 04:56:00 【Xiaobai, a vegetable seller】
One 、 Title Description Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Two 、 Example
Example 1 :
Input : s = "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its length is 3.
Example 2 :
Input : s = "bbbbb"
Output : 1
explain : Because the longest substring without repeating characters is "b", So its length is 1.
3、 ... and 、 Ideas
You can use two pointers left and right The pointer , The right pointer traverses each subscript of the current string , And use a hash table to save the value of the current string and its subscript . If the current value is already in map in , And the subscript of the repeated value is greater than or equal to left Value , At this point, you need to move the left pointer , Move it to the next digit of the duplicate value . And then I kept going map Put values in ( If it is repeated, it will be overwritten ), And constant calculation max Value .
Four 、 Code
/** * @param {string} s * @return {number} */
var lengthOfLongestSubstring = function(s) {
let left = 0;
let max = 0;
let map = new Map()
let length = s.length
for(let right = 0; right < length; right++) {
if(map.has(s[right]) && map.get(s[right]) >= left) {
left = map.get(s[right]) + 1
}
map.set(s[right], right)
max = Math.max(max, right - left + 1)
}
return max
};

5、 ... and 、 analysis
The time complexity is O(n), The space complexity is O(n).
边栏推荐
- 梯度下降法介绍-黑马程序员机器学习讲义
- How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
- Chemical properties and specificity of Worthington Papain
- What is the experience of developing an ice 3D music player in 3 minutes?
- Weak current engineer, 25g Ethernet and 40g Ethernet: which do you choose?
- Redis pipeline technology speed and efficiency increased by 5 times
- Introduction to C language custom types (structure, enumeration, union, bit segment)
- 少儿编程课程改革后的培养方式
- 4G industrial VPN router
- Specificity and correlation of Worthington deoxyribonuclease I
猜你喜欢

MySQL - SQL execution process

External network access SVN server (external network access SVN server deployed on the cloud)

SAP mts/ato/mto/eto topic 8: ATO mode 2 d+ empty mode strategy 85

Training methods after the reform of children's programming course

2022年二级造价工程师备考攻略,你准备好了吗?

An interface testing software that supports offline document sharing in the Intranet
uni-app进阶之认证【day12】

让孩子们学习Steam 教育的应用精髓

apipost接口断言详解

外网访问svn服务器(外网访问部署在云上的svn服务器)
随机推荐
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
What is the role of ECS? How does FTP connect to ECS configuration?
There are many ways to confirm and modify the remote port number
Analyze the actual user groups and demand positioning of distributed database products from the market and demand
Inventory of common tools used by dry goods | data journalists
Physicochemical properties and specificity of Worthington trypsin
Specificity and correlation of Worthington deoxyribonuclease I
2020年Android面试题汇总(中级)
Getattribute return value is null
LeetCode 1662. Check whether two string arrays are equal
ribbon
ribbon
Oracle database prompts no operation permission
SAP MTS/ATO/MTO/ETO专题之七:ATO模式1 M+M模式策略用82(6892)
Abnova peptide design and synthesis solutions
阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展
胶原蛋白酶——Worthington四种类型的粗胶原酶
解析后人类时代类人机器人的优越性
Bi-sql and & or & in
uni-app进阶之认证【day12】