Leetcode: Single Number

来源:互联网 发布:aynak.apk软件下载 编辑:程序博客网 时间:2024/05/16 13:06

Question

Given an array of integers, every element appears twice except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Hide Tags Hash Table Bit Manipulation
Hide Similar Problems (M) Single Number II (M) Single Number III (M) Missing Number


Solution 1

Get idea from here.

class Solution(object):    def singleNumber(self, nums):        """        :type nums: List[int]        :rtype: int        """        if nums==None or len(nums)==0:            return 0        res = nums[0]        for ind in range(1,len(nums)):            res ^= nums[ind]        return res

Solution 2

Get idea from here

python

0 0
原创粉丝点击