Missing Number

LeetCode Q 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.

Solution

Code: Recursive approach

public int missingNumber(int[] nums) {
	int sum = 0;
	for (int num: nums) sum += num;
	return (1 + nums.length) * num.length / 2 - sum;
}

   Reprint policy


《Missing Number》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
 Previous
Ugly Number Ugly Number
LeetCode Q 263 - Ugly NumberWrite a program to check whether a given number is an ugly number. Ugly numbers are positive
2019-04-10 Tong Shi
Next 
Add Digits Add Digits
LeetCode Q 258 - Add DigitsGiven a non-negative integer num, repeatedly add all its digits until the result has only one
2019-04-10 Tong Shi
  TOC