GO题解 | #最长无重复子串#
来自
【GO题解】
611 浏览
0 回复
2021-04-20
最长无重复子串
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
go解题答案
- 思路概括:将子串长度看成双指针维护窗口最大问题
- 思路核心:
1、子串长度可以是用两个指针维护一个最大窗口长度
2、遇到重复,更新start指针,要取历史最大值
3、每次判断是否更新max值和更新重复的最大值func maxLength( arr []int ) int {
// write code here
if len(arr)==0 {
return 0
}
max,start:=0,0
h:=map[int]int{}
for end:=0;end<len(arr);end++{
if h[arr[end]]!=0 {
start=Max(start,h[arr[end]]+1) //历史最大值,因为可能新的重复索引要小于start,更新成重复的索引下一个
}
h[arr[end]]=end
max=Max(max,end-start+1)
}
return max
}
如果有帮助请点个赞哦, 更多文章请看我的博客
题主背景
- 从业8年——超级内卷500Q技术经理——目前专注go和微服务架构