Class Solution

java.lang.Object
g0201_0300.s0268_missing_number.Solution

public class Solution extends Object
268 - Missing Number.<p>Easy</p> <p>Given an array <code>nums</code> containing <code>n</code> distinct numbers in the range <code>[0, n]</code>, return <em>the only number in the range that is missing from the array.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,0,1]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [0,1]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [9,6,4,2,3,5,7,0,1]</p> <p><strong>Output:</strong> 8</p> <p><strong>Explanation:</strong> n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> nums = [0]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> n = 1 since there is 1 number, so all numbers are in the range [0,1]. 1 is the missing number in the range since it does not appear in nums.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 10<sup>4</sup></code></li> <li><code>0 <= nums[i] <= n</code></li> <li>All the numbers of <code>nums</code> are <strong>unique</strong>.</li> </ul> <p><strong>Follow up:</strong> Could you implement a solution using only <code>O(1)</code> extra space complexity and <code>O(n)</code> runtime complexity?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • missingNumber

      public int missingNumber(int[] nums)