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
相關文章
- LeetCode 448. Find All Numbers Disappeared in an ArrayLeetCodeAPP
- #442-Find All Duplicates in an Array-陣列中重複的數字陣列
- Find All Numbers Disappeared in an ArrayAPP
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- [leetcode]remove-duplicates-from-sorted-array-iiLeetCodeREM
- LeetCode Patching Array All In OneLeetCode
- [LeetCode] 80. Remove Duplicates from Sorted Array IILeetCodeREM
- LeetCode 438. Find All Anagrams in a StringLeetCode
- LeetCode 1209. Remove All Adjacent Duplicates in String II 有坑LeetCodeREM
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- [LeetCode] 3011. Find if Array Can Be SortedLeetCode
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 34 Find First and Last Position of Element in Sorted ArrayLeetCodeAST
- Remove-duplicates-from-sorted-arrayREM
- 【leetcode】26. Remove Duplicates from Sorted Array 刪除有序陣列的重複元素LeetCodeREM陣列
- (轉)leetcode:Find All Anagrams in a String 滑動視窗方法總結LeetCode
- B. Find The Array
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode Remove Duplicates型別題目 (python)LeetCodeREM型別Python
- Leetcode Sort ArrayLeetCode
- Find Minimum in Rotated Sorted Array I & II
- [LeetCode] Find the Duplicate NumberLeetCode
- JavaScript object array sort by string bug All In OneJavaScriptObject
- Rotate Array@LeetCodeLeetCode
- [LeetCode] 277. Find the CelebrityLeetCode
- LeetCode 389. Find the DifferenceLeetCode
- LeetCode Kth Largest Element in an ArrayLeetCode
- Leetcode 30 Substring with Concatenation of All WordsLeetCode
- LeetCode之Find Common Characters(Kotlin)LeetCodeKotlin
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- [LeetCode] 2028. Find Missing ObservationsLeetCode
- [leetcode] 890. Find and Replace PatternLeetCode
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- LeetCode之Sort Array By Parity(Kotlin)LeetCodeKotlin
- Leetcode 88. Merge Sorted ArrayLeetCode
- [LeetCode] 3152. Special Array IILeetCode