funclengthOfLongestSubstring(s string)int { max := 0 var runeS = []rune(s) length := len(runeS) for i :=0; i<length; i += 1 { for j := i+1; j <= length; j += 1 { temp := runeS[i:j] same := hasSame(string(temp)) if !same { if max < (j - i) { max = j-i } } } } return max }
/** * 判断一个字符串中是否有重复字符 */ funchasSame(s string)bool { sMap := make(map[string]int) // 字符到index的映射表 sr := []rune(s) for index, v := range sr { _, ok := sMap[string(v)] if ok { returntrue } else { sMap[string(v)] = index } } returnfalse }