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

最长无重复子数组

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

最长无重复子数组

#
# 
# @param arr int整型一维数组 the array
# @return int整型

#
class Solution:
    def maxLength(self , arr ):
        # write code here
        n = len(arr)
        if n <= 1:
            return n
        hashMap = {}
        lo, hi = 0, 0
        res = 0
        while hi < n:
            curr = arr[hi]
            if curr not in hashMap:
                hashMap[curr] = 1

            else:
                while hashMap.has_key(curr) and lo<=hi:
                    del hashMap[arr[lo]]
                    lo += 1
                hashMap[curr] = 1
            res = max(res, hi-lo+1)
            hi += 1
        return res
全部评论

相关推荐

嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务