Leetcode 442. Find All Duplicates in an Array
文章作者:Tyan
部落格:noahsnail.com | CSDN | 簡書
1. Description
2. Solution
- O(n), O(n)
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
if(0 == nums.size()) {
return result;
}
int flag[nums.size()] = {0};
for(int i = 0; i < nums.size(); i++) {
if(flag[nums[i]]) {
result.push_back(nums[i]);
}
else {
flag[nums[i]] = 1;
}
}
return result;
}
};
- O(n), O(1)
class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
if(0 == nums.size()) {
return result;
}
for(int i = 0; i < nums.size(); i++) {
int index = abs(nums[i]) - 1;
if(nums[index] < 0) {
result.push_back(abs(nums[i]));
}
nums[index] = - nums[index];
}
return result;
}
};
Reference
相關文章
- #442-Find All Duplicates in an Array-陣列中重複的數字陣列
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- Find All Numbers Disappeared in an ArrayAPP
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- 448. Find All Numbers Disappeared in an ArrayAPP
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- Leetcode-Remove Duplicates from Sorted Array IILeetCodeREM
- Remove Duplicates from Sorted Array II leetcode javaREMLeetCodeJava
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- LeetCode Patching Array All In OneLeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- LeetCode 438. Find All Anagrams in a StringLeetCode
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- Remove-duplicates-from-sorted-arrayREM
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- 26. Remove Duplicates from Sorted ArrayREM
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 153. Find Minimum in Rotated Sorted ArrayLeetCode
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- [LeetCode] Remove Duplicates from Sorted Array 有序陣列中去除重複項LeetCodeREM陣列
- B. Find The Array
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- (轉)leetcode:Find All Anagrams in a String 滑動視窗方法總結LeetCode
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- [LeetCode] Find Minimum in Rotated Sorted Array (包含遞增和遞減旋轉)LeetCode
- Leetcode Remove Duplicates from Sorted List IILeetCodeREM
- Leetcode-Remove Duplicates from Sorted ListLeetCodeREM
- Remove Duplicates from Sorted List leetcode javaREMLeetCodeJava
- Leetcode Sort ArrayLeetCode
- JavaScript object array sort by string bug All In OneJavaScriptObject