-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pranjal
authored
Aug 28, 2017
1 parent
dfc98cb
commit 0b543eb
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution { | ||
public: | ||
void swap(int& a, int& b){ | ||
int temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
|
||
vector<int> findDuplicates(vector<int>& nums) { | ||
int n = nums.size(), i = 0; | ||
|
||
while(i < n){ | ||
while((nums[i]-1 != i) && (nums[i] != nums[nums[i]-1])){ | ||
swap(nums[i], nums[nums[i]-1]); | ||
} | ||
i++; | ||
} | ||
|
||
vector<int> ans; | ||
|
||
for(int i = 0; i < n; i++){ | ||
if(nums[i]-1 != i){ | ||
ans.push_back(nums[i]); | ||
} | ||
} | ||
|
||
if(ans.size()){ | ||
sort(ans.begin(), ans.end()); | ||
} | ||
|
||
return ans; | ||
} | ||
}; |