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.

    Note:
      Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

  思路:

    这个题其实很简单直接把计算列表没有减少的情况下的总和减去列表减少情况下的总和

  代码:

 1 class Solution(object):
 2     def missingNumber(self, nums):
 3         """
 4         :type nums: List[int]
 5         :rtype: int
 6         """
 7         a = 0
 8         b = 0
 9         for i in nums:
10             a += 1
11             b += i
12         return (1+a)*a//2 - b

 

posted @ 2017-09-18 23:29  唐僧洗发爱飘柔  阅读(180)  评论(0)    收藏  举报