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.
Already Pass Solution
public int MissingNumber(int[] nums) {
int result = nums.Length * (nums.Length + 1) / 2;
foreach(int i in nums)
{
result -= i;
}
return result;
}
解決思路:
(1)利用遺失這個條件,用沒有遺失的狀態求出遺失的部分
(2)使用foreach比for會快一些
待思考:
(1)如果丟失的數字為幾個該如何解決