classSolution { public: vector<int> findDisappearedNumbers(vector<int>& nums){ int n = nums.size(); for (auto& num : nums) { int x = (num - 1) % n; nums[x] += n; } vector<int> ret; for (int i = 0; i < n; i++) { if (nums[i] <= n) { ret.push_back(i + 1); } } return ret; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public List<Integer> findDisappearedNumbers(int[] nums) { intn= nums.length; for (int num : nums) { intx= (num - 1) % n; nums[x] += n; } List<Integer> ret = newArrayList<Integer>(); for (inti=0; i < n; i++) { if (nums[i] <= n) { ret.add(i + 1); } } return ret; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9
classSolution: deffindDisappearedNumbers(self, nums: List[int]) -> List[int]: n = len(nums) for num in nums: x = (num - 1) % n nums[x] += n ret = [i + 1for i, num inenumerate(nums) if num <= n] return ret
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcfindDisappearedNumbers(nums []int) (ans []int) { n := len(nums) for _, v := range nums { v = (v - 1) % n nums[v] += n } for i, v := range nums { if v <= n { ans = append(ans, i+1) } } return }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize) { for (int i = 0; i < numsSize; i++) { int x = (nums[i] - 1) % numsSize; nums[x] += numsSize; } int* ret = malloc(sizeof(int) * numsSize); *returnSize = 0; for (int i = 0; i < numsSize; i++) { if (nums[i] <= numsSize) { ret[(*returnSize)++] = i + 1; } } return ret; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var findDisappearedNumbers = function(nums) { const n = nums.length; for (const num of nums) { const x = (num - 1) % n; nums[x] += n; } const ret = []; for (const [i, num] of nums.entries()) { if (num <= n) { ret.push(i + 1); } } return ret; };