当前位置:网站首页>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).
边栏推荐
- RedHat 8 time synchronization and time zone modification
- 梯度下降法介紹-黑馬程序員機器學習講義
- 事件
- Library management backstage
- Integration of Alibaba cloud SMS services and reasons for illegal message signing
- What are the differences between ECs and virtual hosts? Which is better, ECS or VM?
- 梯度下降法介绍-黑马程序员机器学习讲义
- Ribbon
- ribbon
- Here's all you want to know about takin data (1) startup command
猜你喜欢
随机推荐
2020年Android面试题汇总(中级)
黑马程序员机器学习讲义:线性回归api初步使用
What does IIS mean and what is its function? How does IIS set the size of the web site space on the server?
How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
What are the differences between ECs and virtual hosts? Which is better, ECS or VM?
Bi-sql and & or & in
Automatically convert local pictures to network pictures when writing articles
Bi-sql - Select
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
Introduction to the "penetration foundation" cobalt strike Foundation_ Cobalt strike linkage msfconsole
Introduction à la méthode de descente par Gradient - document d'apprentissage automatique pour les programmeurs de chevaux noirs
How do ECS create FTP accounts? What should I pay attention to during creation?
提pr,push 的时候网络超时配置方法
What does VPS server mean? What is the difference between a VPS server and an ECS?
Zhang Xiaodan, chief architect of Alibaba cloud hybrid cloud: evolution and development of government enterprise hybrid cloud technology architecture
Worthington弹性蛋白酶的应用和相关研究
Replication of variables in golang concurrency
Precautions for online education and training industry filing
阿里云新一代云计算体系架构 CIPU 到底是啥?
『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole








