给定一个数组,包含从 1 到 N 所有的整数,但其中缺了两个数字。你能在 O(N) 时间内只用 O(1) 的空间找到它们吗?
以任意顺序返回这两个数字均可。
输入: [1]
输出: [2,3]
输入: [2,3]
输出: [1,4]
class Solution {
public:
int flag_blank = 30001;
void my_swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
vector<int> missingTwo(vector<int>& nums) {
nums.push_back(flag_blank);
nums.push_back(flag_blank);
int nums_len = nums.size();
vector<int> ret;
int i;
for(i = 0; i < nums_len; i++){
if(nums[i] == i+1){
continue;
}
while(nums[i] != flag_blank && nums[i] != i+1){
my_swap(nums[i], nums[nums[i]-1]);
}
}
for(i = 0; i < nums_len; i++){
if(nums[i] == flag_blank){
ret.push_back(i+1);
}
}
return ret;
}
};
