Leetcode 268 Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

1.XOR

class Solution(object):
    def missingNumber(self, nums):
        t = len(nums)
        for i in range(len(nums)):
            t ^= i
            t ^= nums[i]
        return t

2.SUM

class Solution(object):
    def missingNumber(self, nums):
        return len(nums)*(len(nums)+1)/2-sum(nums)

 

posted @ 2016-05-30 15:36  lilixu  阅读(118)  评论(0编辑  收藏  举报