原题目:https://leetcode-cn.com/problems/missing-number-lcci/
思路:
等差数列求其总和,然后遍历数组,减掉相应的元素,剩下的数就是消失的数
代码:
class Solution {
public:
int missingNumber(vector<int>& nums) {
int n = nums.size();
int sum = n*(n+1)/2;
for (int i:nums) sum -= i;
return sum;
}
};