p268

LeetCode 268 Missing Number 题解

1.题目:

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.

题意:

如题所示找出缺失的那个数

2.解题思路:

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 

public int missingNumber(int[] nums) {

int len = nums.length;
int ans=(1+len)*len/2;
for (int i=0;i<len;i++)
{
ans=ans-nums[i];

}
return ans;
}


4.一些总结: