题解 | #最长无重复子数组#

最长无重复子数组

http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4

package main

/**
 * 
 * @param arr int整型一维数组 the array
 * @return int整型
*/
func maxLength( arr []int ) int {
    // write code here
    left ,right := 0,0
    windows := make(map[int]int)
    max := 0
    for right < len(arr) {
        if _,ok := windows[arr[right]];ok{
            for {
                if arr[left] == arr[right] {
                    delete(windows,arr[left])
                    left++
                    break
                }else{
                    delete(windows,arr[left])
                    left++
                }
            }
        }
        windows[arr[right]]++
        right++
        
        length := right - left
        if length > max {
            max = length
        }
    }
  
    return max
    
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务