当前位置:网站首页>Longest string without duplicate characters (leetcode 3)
Longest string without duplicate characters (leetcode 3)
2022-06-12 11:56:00 【Liyihe beats Xiao Tai】
Author URI : Li Yihe beat Xiao Tai's blog
* Personal introduction : Hello everyone , I'm Li Yihe !( ̄▽ ̄)~*
Remember the praise. 、 Collection 、 Comment on ️️️
Study carefully !!!
List of articles
Given a string s , Please find out that there are no duplicate characters in it Longest substrings The length of .
Solution 1 : The sliding window
Because to find the longest string that is not repeated , We need two pointers , The left pointer is fixed first , The pointers on the right are added to the set one by one , Determine whether there is a duplicate value each time , If so, it's over , Judge whether the length of this time is longer than that of the last time , Then the left pointer moves one... To the right , Then the right pointer continues the above operation until the end .
public int lengthOfLongestSubstring(String s) {
Set<Character> set=new HashSet<Character>();
int length=s.length();
int rigth=0,max=0;
for(int i=0;i<length;i++){
if(i!=0)
set.remove(s.charAt(i-1));
while(rigth<length&&!set.contains(s.charAt(rigth))){
set.add(s.charAt(rigth));
rigth++;
}
max=Math.max(max,rigth-i);
}
return max;
}
边栏推荐
- Go sends SMS based on alicloud
- LeetCode 890. Find and replace mode (analog + double hash table)
- Cookie和Session
- Blue Bridge Cup 2015 CA provincial competition (filling the pit)
- IP address management
- 为什么新品发布上架之后会没有流量,新品应该怎么发布?
- Load/store memory access instruction of arm instruction set (1)
- ARM指令集之Load/Store访存指令(一)
- 邻居子系统之ARP协议数据处理过程
- JS to load and display Excel files
猜你喜欢

Logrotate log rotation method create and copyruncate principles

manuscript手稿格式准备

LeetCode 890. 查找和替换模式(模拟+双哈希表)

LeetCode 497. 非重叠矩形中的随机点(前缀和+二分)

Lambda and filter, index of list and numpy array, as well as various distance metrics, concatenated array and distinction between axis=0 and axis=1

必杀技--使用FFmpeg命令快速精准剪切视频

C# 37. Textbox scroll bar and multiline

PDSCH 相关

The second day of QML study

6.6 分离卷积
随机推荐
Neighbor item status update of neighbor subsystem
TinyMCE series (II) TinyMCE plug-in development
MySQL - built in function
机器学习之决策树
First understand the onion model, analyze the implementation process of middleware, and analyze the source code of KOA Middleware
TinyMCE series (IV) introduction to common built-in UI components of TinyMCE
判断网络文件是否存在,获取网络文件大小,创建时间、修改时间
TinyMCE series (III) introduction to common TinyMCE APIs
Pseudo instruction of arm instruction set
机器学习之线性模型
Tpage design
QML学习 第一天
B.刷墙(C语言)
Socket implements TCP communication flow
6.6 separate convolution
【QNX Hypervisor 2.2 用户手册】4.1 构建QNX Hypervisor系统的方法
6.6 分离卷积
ARM指令集之乘法指令
Load/store memory access instruction of arm instruction set (1)
Channel shuffle class
