268 Missing Number

原创 2018年04月12日 15:54:53

Missing Number
数组中包含n个0-N不同元素,找到缺失的元素

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int sum = (1 + nums.size())*nums.size()/2;
        int s = 0;
        for (int i = 0; i < nums.size(); i++){
            s += nums[i];
        }
        return sum - s;
    }
};

solution中另一种位XOR解法:
以[0, 1, 3, 4]为例:

Index 0 1 2 3
Value 0 1 3 4

missing=4(00)(11)(23)(34)
=(44)(00)(11)(33)2=2

#Python:
class Solution:
    def missingNumber(self, nums):
        missing = len(nums)
        for i, num in enumerate(nums):
            missing ^= i ^ num
        return missing

【LeetCode】268 Missing Number (java实现)

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin...
  • styshoo
  • styshoo
  • 2015-08-29 12:28:01
  • 789

leetcode 268: Missing Number

Missing Number Total Accepted: 10033 Total Submissions: 31720 Given an array containin...
  • xudli
  • xudli
  • 2015-09-08 09:19:12
  • 2268

<LeetCode OJ> 268. Missing Number

268. Missing Number My Submissions Question Total Accepted: 31740 Total Submissions: 83547 Diffi...
  • EbowTang
  • EbowTang
  • 2016-01-04 19:22:42
  • 2159

leetcode:268. Missing Number(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Missing NumberGiven an array containing n distinct numbers taken...
  • u012975705
  • u012975705
  • 2016-03-11 22:07:30
  • 2291

leetcode【268 Missing Number】【Python】

给出从0到n的n+1个数字中的n个数,找到缺失的那个数字。第一想法就遍历就可以了,但是注意不要被它给的例子所迷惑,数组不一定是排好序的,所以需要先排序。class Solution(object): ...
  • u014251967
  • u014251967
  • 2016-09-08 17:22:13
  • 946

[leetcode] 268.Missing Number

题目: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is mis...
  • u014673347
  • u014673347
  • 2015-08-24 10:30:06
  • 490

LeetCode 268 Missing Number(丢失的数字)

翻译给定一个包含从0,1,2...,n中取出来的互不相同的数字,从数组中找出一个丢失的数字。例如, 给定nums = [0, 1, 3] 返回2。备注: 你的算法应该运行在线性时间复杂度下。你可以只用...
  • NoMasp
  • NoMasp
  • 2016-02-29 22:27:09
  • 4090

LeetCode 268. Missing Number(缺失数字)

原题网址:https://leetcode.com/problems/missing-number/ Given an array containing n distinct numbers...
  • jmspan
  • jmspan
  • 2016-04-11 02:15:40
  • 428

[268]Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is miss...
  • u013933371
  • u013933371
  • 2015-08-25 22:34:32
  • 67

leetcode 268 : Missing Number

1、原题如下: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is...
  • l3368bcttqnqn
  • l3368bcttqnqn
  • 2015-10-27 15:07:22
  • 141
收藏助手
不良信息举报
您举报文章:268 Missing Number
举报原因:
原因补充:

(最多只允许输入30个字)