LeetCode Contains Duplicate

BabbleDay posted @ 2015年7月16日 15:32 in 刷题防身 with tags python array leetcode Contains Duplicate , 553 阅读

位操作 O(n)

class Solution:
    # @param {integer[]} nums
    # @return {boolean}
    def containsDuplicate(self, nums):
        pos, neg = 0, 0
        for x in nums:
            if x < 0:
                temp = 1 << -x
                if neg & temp != 0:
                    return True
                pos |= temp
            else:
                temp = 1 << x
                if pos & temp != 0:
                    return True
                pos |= temp
        return False

哈希表 O(n)

Python一行

Avatar_small
Things to do 说:
2024年2月28日 00:12

I have always loved research and it's been actually the cause of my successful finding about all the fun things to do. I used to be doing a search on the net, I discovered a wonderful post regarding the subject of the article thatt I will have to share with your followers. Don't have any ideas for holiday? There are plenty of things to do post around your location that you can find there.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter