这个题只需要用到数学思想即可解决。上代码:
class Solution {
public int missingNumber(int[] nums) {
int L = nums.length;
int sumOfAll = 0, sumOfNums = 0;
for(int i = 1; i <= L; ++i){
sumOfAll += i;
}
for(int elem : nums){
sumOfNums += elem;
}
return sumOfAll - sumOfNums;
}
}
网友评论