当前位置:网站首页>無重複字符的最長字符串(LeetCode 3)
無重複字符的最長字符串(LeetCode 3)
2022-06-12 11:56:00 【李奕赫揍小邰】
作者主頁:李奕赫揍小邰的博客
* 個人介紹:大家好,我是李奕赫!( ̄▽ ̄)~*
記得點贊、收藏、評論️️️
認真學習!!!
文章目錄
給定一個字符串 s ,請你找出其中不含有重複字符的 最長子串 的長度。
解法一:滑動窗口
因為要找到不重複的最長字符串,我們需要兩個指針,左邊的指針先固定,右邊的指針一個一個加入集合中,每次都判斷一次是否有重複的值,如果有就結束,判斷這次的長度跟上次的長度誰長,然後左指針向右移動一個,然後右指針繼續上述操作直至結束。
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;
}
边栏推荐
- NVIDIA Jetson Nano Developer Kit 入门
- Cookies and sessions
- Google Earth Engine(GEE)——Kmeans聚类快速进行土地分类(双for循环快速调参)
- 必杀技--使用FFmpeg命令快速精准剪切视频
- 判断网络文件是否存在,获取网络文件大小,创建时间、修改时间
- Spark常用封装类
- 5g NR protocol learning -- ts38.211 downlink channel
- conda环境下pip install 无法安装到指定conda环境中(conda环境的默认pip安装位置)
- K52. Chapter 1: installing kubernetes v1.22 based on kubeadm -- cluster deployment
- 为什么新品发布上架之后会没有流量,新品应该怎么发布?
猜你喜欢

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

Who moved my package lock

Logrotate log rotation method create and copyruncate principles

【深度学习基础】神经网络的学习(4)

IP address management

The second day of QML study

LeetCode 497. Random points in non overlapping rectangles (prefix and + bisection)

6.6 分离卷积

MySQL - built in function

Cookies and sessions
随机推荐
Byte order - how to judge the big end and the small end
ioremap
Basic principle of Doppler effect
Jump instruction of arm instruction set
Who moved my package lock
Manuscript manuscript format preparation
M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
System. IO. Fileloadexception exception
Pseudo instruction of arm instruction set
Design of tablewithpage
邻居子系统之邻居项状态更新
ARM指令集之Load/Store访存指令(一)
Relatively rare exception records in UI automation test
影响店铺权重的三十一条,快来看看中招了吗
ARM processor mode and register
Channel Shuffle类
TinyMCE series (I) TinyMCE environment construction
一个人必须不停地写作,才能不被茫茫人海淹没。
Unlimited growth, we will all go to the future | the 15th anniversary of the founding of InfoQ China
Load/store access instruction of arm instruction set (2)
