当前位置:网站首页>无重复字符的最长子串
无重复字符的最长子串
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
}
边栏推荐
- Pyhton实战汇总篇
- 1009 Product of Polynomials C语言多项式乘积(25分)
- Unix Environment Programming Chapter 15 15.3 Functions popen and pclose
- unix环境编程 第十五章 15.9 共享存储
- C语言统计不同单词数
- Official explanation, detailed explanation and example of torch.cat() function
- 从位图到布隆过滤器
- unix环境编程 第十五章 15.6 XSI IPC
- Solve the ali cloud oss - the original 】 【 exe double-click response can't open, to provide a solution
- 获取指定年度所有周的工具类
猜你喜欢
随机推荐
MySQL查询性能优化七种武器之索引潜水
unix环境编程 第十五章 15.7消息队列
torch.cat()函数的官方解释,详解以及例子
Unix Environment Programming Chapter 14 14.4 I/O Multiplexing
tensor.eq() tensor.item() tensor.argmax()
通过Doc在MySQL数据库中建表
MySQL和MyEclipse的数据库连接操作
unix环境编程 第十五章 15.3 函数popen和pclose
微信小程序——天气查询
获取指定年度所有周的工具类
可能95%的人还在犯的PyTorch错误
unix环境编程 第十五章 15.9 共享存储
【 original 】 VMware Workstation implementation Openwrt soft routing, the ESXI, content is very detailed!
OneNote 教程,如何在 OneNote 中搜索和查找笔记?
Quartz分布式实现
c语言函数的递归调用(汉诺塔问题,楼梯递归问题等)
Beauty Values
1009 Product of Polynomials C语言多项式乘积(25分)
研发需求的验收标准应该怎么写? | 敏捷实践
Unix Environment Programming Chapter 15 15.9 Shared Storage