题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
package main /** * * @param arr int整型一维数组 the array * @return int整型 */ func maxLength( arr []int ) int { // write code here maxLen := 0 len := 0 left := -1 valueMap := make(map[int]int) for idx, a := range arr { if valueIdx, ok := valueMap[a]; !ok || valueMap[a] == -1 { len++ } else { if valueIdx > left { left = valueIdx } len = idx - left } valueMap[a] = idx if maxLen < len { maxLen = len } } return maxLen }