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后添加至返回容器即可。

posted on 2018-05-30 15:57  高数考了59  阅读(114)  评论(0)    收藏  举报