classSolution: deffindDuplicates(self, nums: List[int]) -> List[int]: for i inrange(len(nums)): while nums[i] != nums[nums[i] - 1]: nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1] return [num for i, num inenumerate(nums) if num - 1 != i]
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: vector<int> findDuplicates(vector<int>& nums){ int n = nums.size(); for (int i = 0; i < n; ++i) { while (nums[i] != nums[nums[i] - 1]) { swap(nums[i], nums[nums[i] - 1]); } } vector<int> ans; for (int i = 0; i < n; ++i) { if (nums[i] - 1 != i) { ans.push_back(nums[i]); } } return ans; } };
publicclassSolution { public IList<int> FindDuplicates(int[] nums) { int n = nums.Length; for (int i = 0; i < n; ++i) { while (nums[i] != nums[nums[i] - 1]) { Swap(nums, i, nums[i] - 1); } } IList<int> ans = new List<int>(); for (int i = 0; i < n; ++i) { if (nums[i] - 1 != i) { ans.Add(nums[i]); } } return ans; }
publicvoidSwap(int[] nums, int index1, int index2) { int temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = temp; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcfindDuplicates(nums []int) (ans []int) { for i := range nums { for nums[i] != nums[nums[i]-1] { nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i] } } for i, num := range nums { if num-1 != i { ans = append(ans, num) } } return }
classSolution: deffindDuplicates(self, nums: List[int]) -> List[int]: ans = [] for x in nums: x = abs(x) if nums[x - 1] > 0: nums[x - 1] = -nums[x - 1] else: ans.append(x) return ans
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: vector<int> findDuplicates(vector<int>& nums){ int n = nums.size(); vector<int> ans; for (int i = 0; i < n; ++i) { int x = abs(nums[i]); if (nums[x - 1] > 0) { nums[x - 1] = -nums[x - 1]; } else { ans.push_back(x); } } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public List<Integer> findDuplicates(int[] nums) { intn= nums.length; List<Integer> ans = newArrayList<Integer>(); for (inti=0; i < n; ++i) { intx= Math.abs(nums[i]); if (nums[x - 1] > 0) { nums[x - 1] = -nums[x - 1]; } else { ans.add(x); } } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { public IList<int> FindDuplicates(int[] nums) { int n = nums.Length; IList<int> ans = new List<int>(); for (int i = 0; i < n; ++i) { int x = Math.Abs(nums[i]); if (nums[x - 1] > 0) { nums[x - 1] = -nums[x - 1]; } else { ans.Add(x); } } return ans; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcfindDuplicates(nums []int) (ans []int) { for _, x := range nums { if x < 0 { x = -x } if nums[x-1] > 0 { nums[x-1] = - nums[x-1] } else { ans = append(ans, x) } } return }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var findDuplicates = function(nums) { const n = nums.length; const ans = []; for (let i = 0; i < n; ++i) { const x = Math.abs(nums[i]); if (nums[x - 1] > 0) { nums[x - 1] = -nums[x - 1]; } else { ans.push(x); } } return ans; }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int* findDuplicates(int* nums, int numsSize, int* returnSize) { int *ans = (int *)malloc(sizeof(int) * numsSize); int pos = 0; for (int i = 0; i < numsSize; ++i) { int x = abs(nums[i]); if (nums[x - 1] > 0) { nums[x - 1] = -nums[x - 1]; } else { ans[pos++] = x; } } *returnSize = pos; return ans; }