0645.cpp (414B)
1 class Solution { 2 public: 3 vector<int> findErrorNums(vector<int> &nums) const { 4 const int n = size(nums); 5 int dbl = 0, sum = 0; 6 for (int i = 0; i < n; i++) { 7 const int num = abs(nums[i]); 8 if (nums[num - 1] < 0) dbl = num; 9 nums[num - 1] = -nums[num - 1]; 10 sum += num; 11 } 12 return {dbl, n * (n + 1) / 2 - (sum - dbl)}; 13 } 14 };