题目
计算出给定字符串中的最长子串长度
思路
- 使用一个map结构存储每个字符上一次出现的坐标,然后设置一个start变量用来定位最长子串的开头,如果字符上一次出现的坐标大于start即重复字符出现了,更改start重新计算长度
实现
func findLongestStr(str string){
start := 0
maxLength := 0
lastShowIndex := make(map[byte] int)
for i,ch := range [] byte(str) {
if lastI,ok := lastShowIndex[ch];ok&&lastI >=start{
start = i+1
}
if i-start+1>maxLength{
maxLength = i-start+1
}
lastShowIndex[ch] = i
}
return maxLength
}