Missing Number

来源:互联网 发布:淘宝新店如何运营 编辑:程序博客网 时间:2024/05/16 08:33

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.

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

0 0
原创粉丝点击