https://leetcode.cn/problems/longest-substring-without-repeating-characters
话不多说,直接上代码
func lengthOfLongestSubstring(s string) int {
var repeatMap = map[rune]bool{}
var returnNum = 0
var higestNum = 0
var repeatSlice = []rune{}
for k, stringNum := range s {
if _, ok := repeatMap[stringNum]; !ok {
repeatSlice = append(repeatSlice, stringNum)
repeatMap[stringNum] = true
returnNum += 1
continue
}
if returnNum > higestNum {
higestNum = returnNum
}
for k, v := range repeatSlice {
if v == stringNum {
repeatSlice = append(repeatSlice[k+1:], stringNum)
break
}
delete(repeatMap, v)
}
returnNum = len(repeatMap)
}
if returnNum > higestNum {
return returnNum
}
return higestNum
}
再来就是效率图
总结一下今天的解体,对比之前现在一开始就会往map和slice合作过滤去解题。而不是纯用for去解题
面对复杂问题时,结合不同的数据结构和算法,往往会有更好的解决方案
后面再回顾。应该就是再次提升内存的使用和运行速度的方面