同https://blog.youkuaiyun.com/ShellDawn/article/details/109499135
/*
* @lc app=leetcode id=448 lang=cpp
*
* [448] Find All Numbers Disappeared in an Array
*/
// @lc code=start
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
int N = nums.size();
int M = N + 1;
int t;
for(int i=0;i<N;i++){
t = nums[i] % M - 1;
nums[t] += M;
}
vector<int> ans;
for(int i=0;i<N;i++){
t = nums[i] / M;
if(t == 0) ans.push_back(i+1);
}
return ans;
}
};
// @lc code=end
本文提供了一种寻找数组中消失元素的有效算法。通过修改数组自身来标记出现过的数字,并最终找到未被标记的数字即为消失的元素。这种方法巧妙地利用了数组空间,避免了额外的空间开销。

被折叠的 条评论
为什么被折叠?



