当前位置:网站首页>Longest string without duplicate characters
Longest string without duplicate characters
2022-07-29 05:32:00 【Xiao Hong is working hard】

This is a topic of Li Kou , At first, I tried to solve it with dynamic programming , Yes, yes , But after reading the solution, I think sliding window is also a wonderful solution , After studying, I made a summary .
1. Traversal string , And define another pointer , Initialize the pointer to the leftmost side of the string
2. Judge whether the character pointed to by the current pointer has appeared in the set .
1) If not , Add this character to the set , And keep the pointer moving to the next character ;
2) If it does , Stop moving .
3. Get the current cut-off string length , And compare with the previous string .
Flow chart :
The longest string length can be seen intuitively from the process .
Sliding windows is like queues , The first one who comes in is the first to leave after finding repeated characters , Then recycle judgment .
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int index = 0;// Define the right pointer
int max = 0;
for(int i = 0;i < s.length();i++){
if(i!=0){
set.remove(s.charAt(i-1));
}
while(index < s.length()&&!set.contains(s.charAt(index))) {
set.add(s.charAt(index));
index++;
}
max = Math.max(max,index - i);
}
return max;
}
}
边栏推荐
- 携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”
- 省市区三级联动(简单又完美)
- Live broadcast Preview: integration of JD cloud Devops and jfrog product library
- The road to success in R & D efficiency of 1000 person Internet companies
- 适创科技以云仿真平台,支持“中国智造”升级
- C语言 一级指针
- 数据库操作 Day 6
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- Common shortcut keys for Ad
- Cmu15-213 malloc lab experiment record
猜你喜欢

【C语言系列】—深度解剖数据在内存中的存储(二)-浮点型

AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带

Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories

During the appointment, the 2022 JD cloud industrial integration new product launch was launched online

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

抢先预约 | 阿里云无影云应用线上发布会预约开启

直播预告:京东云DevOps与JFrog制品库的融合

Detailed explanation of serial port communication

Helm chart for Kubernetes

365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
随机推荐
C语言 一级指针
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
Why is Google's internal tools not suitable for you?
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
英伟达周锡健:设计到数字营销的最后一公里
vim编辑器使用
321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
Day 5
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
【活动预告】云上数字工厂与中小企业数字化转型创新论坛
存储类别
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
Occt learning 003 - MFC single document project
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
冒泡排序 C语言
递归的基本原理
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
365天挑战LeetCode1000题——Day 036 二叉树剪枝 + 子数组和排序后的区间和 + 删除最短的子数组使剩余数组有序
C language n queen problem