当前位置:网站首页>Leetcode-3 longest substring without duplicate characters
Leetcode-3 longest substring without duplicate characters
2022-04-22 13:48:00 【Cc824 constant】
Example 1:
Input : s = "abcabcbb"
Output : 3
explain : Because the longest substring without repeating characters is "abc", So its The length is 3.
At first, I used violence myself , I found that some use cases tested, but , The time complexity is still relatively high
I learned a new way to solve this problem —— Slide window idea
hypothesis s=“pwwkew”
Use the left and right pointers , Move the right pointer right to join the window , Move the left pointer right out of the window .
First judge hashset Whether it contains an element , If not, join set in , The right pointer continues to move right , Repeat the process ;
Code : The time complexity is O(n) n by s The length of
class Solution {
public int lengthOfLongestSubstring(String s) {
HashSet<Character> hash=new HashSet<>();
int len=s.length();
int count=0,left=-1; // On the left side of the window
for(int i=0;i<len;i++) {
if(i!=0)
hash.remove(s.charAt(i-1)); Gradually delete the data in the window
while(left+1<len&&!hash.contains(s.charAt(left+1))) { // If the number is not in the window
hash.add(s.charAt(left+1)); // Just join the window
left++;
}
count=Math.max(count, left-i+1); // Update window length
}
return count;
}
}
版权声明
本文为[Cc824 constant]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221347257596.html
边栏推荐
- The vscode database calls the stored procedure and unrecognized data appears
- 翻译翻译什么是适配器模式?
- eBPF学习 - 入门
- Introduction notes to PHP zero Foundation (12): array
- C 7.0 use underline to ignore variables used
- FreeRTOS快速入门-初探FreeRTOS
- CDF全球调查:软件交付性能停滞不前
- Redis (VI) - list of common data types of redis
- MySQL DNS解析和主机缓存
- Add pyuic to pychar's external tool
猜你喜欢

Understand efficientnet

Socket的阻塞模式和非阻塞模式

Specify the parameter serialization component in the swagger interface document as newtonsoft Json

Add pyrcc5 to pychar's external tool

Genesis公链赋予DAO全新生命力

Chapitre sur la promotion précise des investissements dans la gestion de l'exploitation numérique des parcs industriels

"Enterprise service" of digital operation and management of Industrial Park

堆排序详解(C语言)

自动化测试的生命周期是什么?

How does redis view the memory size occupied by a single key
随机推荐
io_uring技术在分布式云原生数据库中的应用
MySQL uses stored procedures to add data
html文件中通过meta信息控制缓存有效果吗?目前用的多吗?
国元期货开户是否可靠?资金安全吗?
Redis (IV) - string of common data types of redis
Genesis public chain gives Dao new vitality
关于QPS、TPS、并发用户数及吞吐量浅谈
Database resource load management (Part 2)
Soochow securities x kangaroo cloud: the data is easily available and has millisecond response ability. What did Soochow securities do right?
《深入 C 语言和程序运行原理》11 标准库:深入理解标准 IO(学习笔记)
11 standard library of in-depth C language and program operation principle: in-depth understanding of standard IO (learning notes)
FreeRTOS quick start
Fizz企业级微服务网关-服务编排,祭出终结BFF层的大杀器
5分钟快速了解MySQL索引的各种类型
回忆,这一年(华师918血泪贴)
Recall, this year (Huashi 918 blood and tears paste)
BCC-funccount
堆排序详解(C语言)
智汀如何连接小米智能音箱?
BAIC Foton, Sinopec and light engineering Internet of things set up Sinopec to sell hydrogen energy (Beijing)