当前位置:网站首页>无重复字符的最长子串
无重复字符的最长子串
2022-08-09 10:51:00 【ase2014】
题目
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度
示例 1:
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
题解
- 使用滑动窗口实现
- 实际就为操作左右两个index left, right,right一直增加,left为当right出现重复的时候,left调为之前老的index+1
- 而且old index必须大于等于left index(即存在的必须在left ~ right之间)
代码
func lengthOfLongestSubstring(s string) int {
sLen := len(s)
if sLen < 2 {
return sLen
}
result := 0
l, r := 0, 0
flag := make(map[int32]int, sLen)
for i, v := range s {
o, ok := flag[v]
// 存在的字母的index必须在[l, r]内
if ok && o >= l {
// 将l设置为o + 1,即存在的字母的下一个
l = o + 1
}
flag[v] = i
r += 1
tmp := r - l
if result < tmp {
result = tmp
}
}
return result
}
边栏推荐
猜你喜欢
随机推荐
faster-rcnn中的RPN原理
Dialogue with the DPO of a multinational consumer brand: How to start with data security compliance?See you on 8.11 Live!
我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
备份mongodb数据库(认证)
unix环境编程 第十五章 15.10 POSIX信号量
真香!肝完Alibaba这份面试通关宝典,我成功拿下今年第15个Offer
jmeter BeanShell 后置处理器
依赖注入(Dependency Injection)框架是如何实现的
Unix Environment Programming Chapter 15 15.9 Shared Storage
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization论文阅读
kubernetes中不可见的OOM
【原创】JPA中@PrePersist和@PreUpdate的用法
Pyhton实战汇总篇
通过Doc在MySQL数据库中建表
unix环境编程 第十四章 14.4 I/O多路转接
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 2 Validate input: letters and numbers only
【原创】解决阿里云oss-browser.exe双击没反应打不开,提供一种解决方案
TensorFlow: NameError: name 'input_data' is not defined
Netscope: Online visualization tool for neural network structures
jvm-类加载系统









