题目描述
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
代码
func lengthOfLongestSubstring(s string) int {
// 判断非空
if s == "" {
return 0
}
max := 1 // 记录最大次数
afterIndex := 1 // 利用切片,需要后面下标
frontIndex := 0 // 利用切片,需要前面下标
for frontIndex < len(s) {
for afterIndex < len(s){
// 字符不存在则加一,否则退出两下标都加一
if isHaveChar(s[frontIndex:afterIndex],s[afterIndex]) {
break
} else {
afterIndex++
}
}
if max < afterIndex - frontIndex {
// 判断本次的长度和最大长度内个长
max = afterIndex - frontIndex
}
frontIndex ++
}
return max
}
// 判断字符串中是否有特定字符
func isHaveChar(s string,n uint8 ) bool {
for _, i := range s {
if uint8(i) == n{
return true
}
}
return false
}