Tuesday, November 17, 2015

[leetcode] Missing Number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class Solution {
    public int missingNumber(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++){
         sum += nums[i];
        }
        int expectSum = (0+nums.length)*(nums.length+1)/2;
        return expectSum-sum;
    }
}

No comments:

Post a Comment