1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 vector<int> findDisappearedNumbers(vector<int>& nums) 12 { 13 int sz=nums.size(); 14 vector<int> res; 15 for(int i=0;i<sz;i++) 16 { 17 int loc=abs(nums[i])-1; 18 nums[loc]=nums[loc]<0? nums[loc]:-nums[loc]; 19 } 20 for(int j=0;j<sz;j++) 21 if(nums[j]>0) 22 res.push_back(j+1); 23 return res; 24 } 25 };
用数组中的元素作为下标,将对应下标上的元素全部添加负号,这样一来,消失数字不会出现在数组中,那么它在数组中对应的数字也不会被加负号,将下标+1后添加至返回容器即可。
浙公网安备 33010602011771号