/*
* @lc app=leetcode id=442 lang=cpp
*
* [442] Find All Duplicates in an Array
*/
// @lc code=start
class Solution {
public:
vector<int> findDuplicates(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 > 1) ans.push_back(i+1);
}
return ans;
}
};
// @lc code=end
No.253 - [442] Find All Duplicates in an Array - 下标来hash
最新推荐文章于 2024-06-26 20:08:03 发布