//所谓的原地算法,其实就是修改数据,将数据成倍修改,跟状态数组的原理不就是一样的么。。
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int n = nums.size();
for (auto& num : nums) {
int x = (num - 1) % n;
nums[x] += n;
}
vector<int> ret;
for (int i = 0; i < n; i++) {
if (nums[i] <= n) {
ret.push_back(i + 1);
}
}
return ret;
}
};
In this question, subtracting n, and then filtering the numbers greater than or equal to 0 must be correct, but the experiment actually showed a runtime error. This may be the problem of the subject itself.
Reorganize the matrix template
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size();//行数目
int n = nums[0].size();//列的数目
if (m * n != r * c) {
//若不相等则不可能
return nums;
}
vector<vector<int>> ans(r, vector<int>(c));//答案vector
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = nums[x / n][x % n];//
}
return ans;
}
};