新建一个数组 res 用来保存排序完毕的数组。遍历两次 nums,第一次遍历时把所有偶数依次追加到 res 中,第二次遍历时把所有奇数依次追加到 res 中。
代码
[sol1-Python3]
1 2 3
classSolution: defsortArrayByParity(self, nums: List[int]) -> List[int]: return [num for num in nums if num % 2 == 0] + [num for num in nums if num % 2 == 1]
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public: vector<int> sortArrayByParity(vector<int>& nums){ vector<int> res; for (auto & num : nums) { if (num % 2 == 0) { res.push_back(num); } } for (auto & num : nums) { if (num % 2 == 1) { res.push_back(num); } } return res; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicint[] sortArrayByParity(int[] nums) { intn= nums.length, index = 0; int[] res = newint[n]; for (int num : nums) { if (num % 2 == 0) { res[index++] = num; } } for (int num : nums) { if (num % 2 == 1) { res[index++] = num; } } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicint[] SortArrayByParity(int[] nums) { int n = nums.Length, index = 0; int[] res = newint[n]; foreach (int num in nums) { if (num % 2 == 0) { res[index++] = num; } } foreach (int num in nums) { if (num % 2 == 1) { res[index++] = num; } } return res; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int* sortArrayByParity(int* nums, int numsSize, int* returnSize) { int *res = (int *)malloc(sizeof(int) * numsSize), index = 0; for (int i = 0; i < numsSize; i++) { if (nums[i] % 2 == 0) { res[index++] = nums[i]; } } for (int i = 0; i < numsSize; i++) { if (nums[i] % 2 == 1) { res[index++] = nums[i]; } } *returnSize = numsSize; return res; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcsortArrayByParity(nums []int) []int { ans := make([]int, 0, len(nums)) for _, num := range nums { if num%2 == 0 { ans = append(ans, num) } } for _, num := range nums { if num%2 == 1 { ans = append(ans, num) } } return ans }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var sortArrayByParity = function(nums) { let n = nums.length, index = 0; const res = newArray(n).fill(0); for (const num of nums) { if (num % 2 === 0) { res[index++] = num; } } for (const num of nums) { if (num % 2 === 1) { res[index++] = num; } } return res; };
新建一个长度为 n 的数组 res 用来保存排完序的数组。遍历一遍 nums,遇到偶数则从 res 左侧开始替换元素,遇到奇数则从 res 右侧开始替换元素。遍历完成后,res 就保存了排序完毕的数组。
代码
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defsortArrayByParity(self, nums: List[int]) -> List[int]: n = len(nums) res, left, right = [0] * n, 0, n - 1 for num in nums: if num % 2 == 0: res[left] = num left += 1 else: res[right] = num right -= 1 return res
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: vector<int> sortArrayByParity(vector<int>& nums){ int n = nums.size(); vector<int> res(n); int left = 0, right = n - 1; for (auto & num : nums) { if (num % 2 == 0) { res[left++] = num; } else { res[right--] = num; } } return res; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { publicint[] sortArrayByParity(int[] nums) { intn= nums.length; int[] res = newint[n]; intleft=0, right = n - 1; for (int num : nums) { if (num % 2 == 0) { res[left++] = num; } else { res[right--] = num; } } return res; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicint[] SortArrayByParity(int[] nums) { int n = nums.Length; int[] res = newint[n]; int left = 0, right = n - 1; foreach (int num in nums) { if (num % 2 == 0) { res[left++] = num; } else { res[right--] = num; } } return res; } }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
int* sortArrayByParity(int* nums, int numsSize, int* returnSize) { int *res = (int *)malloc(sizeof(int) * numsSize); int left = 0, right = numsSize - 1; for (int i = 0; i < numsSize; i++) { if (nums[i] % 2 == 0) { res[left++] = nums[i]; } else { res[right--] = nums[i]; } } *returnSize = numsSize; return res; }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcsortArrayByParity(nums []int) []int { n := len(nums) ans := make([]int, n) left, right := 0, n-1 for _, num := range nums { if num%2 == 0 { ans[left] = num left++ } else { ans[right] = num right-- } } return ans }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var sortArrayByParity = function(nums) { const n = nums.length; const res = newArray(n).fill(0); let left = 0, right = n - 1; for (const num of nums) { if (num % 2 === 0) { res[left++] = num; } else { res[right--] = num; } } return res; };
classSolution: defsortArrayByParity(self, nums: List[int]) -> List[int]: left, right = 0, len(nums) - 1 while left < right: while left < right and nums[left] % 2 == 0: left += 1 while left < right and nums[right] % 2 == 1: right -= 1 if left < right: nums[left], nums[right] = nums[right], nums[left] left += 1 right -= 1 return nums
[sol3-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: vector<int> sortArrayByParity(vector<int>& nums){ int left = 0, right = nums.size() - 1; while (left < right) { while (left < right and nums[left] % 2 == 0) { left++; } while (left < right and nums[right] % 2 == 1) { right--; } if (left < right) { swap(nums[left++], nums[right--]); } } return nums; } };
int* sortArrayByParity(int* nums, int numsSize, int* returnSize) { int left = 0, right = numsSize - 1; while (left < right) { while (left < right && nums[left] % 2 == 0) { left++; } while (left < right && nums[right] % 2 == 1) { right--; } if (left < right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; left++; right--; } } *returnSize = numsSize; return nums; }
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcsortArrayByParity(nums []int) []int { left, right := 0, len(nums)-1 for left < right { for left < right && nums[left]%2 == 0 { left++ } for left < right && nums[right]%2 == 1 { right-- } if left < right { nums[left], nums[right] = nums[right], nums[left] left++ right-- } } return nums }
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var sortArrayByParity = function(nums) { let left = 0, right = nums.length - 1; while (left < right) { while (left < right && nums[left] % 2 === 0) { left++; } while (left < right && nums[right] % 2 === 1) { right--; } if (left < right) { const temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } return nums; };