922. Sort Array By Parity II # 题目# Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer...
1classSolution {2privatevoidswap(int[] A,inti,intj) {3inta =A[i];4A[i] =A[j];5A[j] =a;6}78publicint[] sortArrayByParity(int[] A) {9for(intleft = 0, right = A.length-1; left <right; ) {10if(A[left]%2 == 1 && A[right]%2 == 0) {11swap(A, left, right);12...
public void swap(int [] Array,int i,int j){ int temp=Array[i]; Array[i]=Array[j]; Array[j]=temp; }
A: Yes, the function can modify the input array in-place and should return the modified array. Q: What should the function return if the input array is empty? A: If the array is empty, the function should return an empty array. The function sort_by_parity() should take an integer ar...
LeetCode-Sort Array By Parity Description: Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition....
[LeetCode] 922. Sort Array By Parity II (C++) [LeetCode] 922. Sort Array By Parity II (C++) Easy Share Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that wheneve......
public int[] sortArrayByParityII(int[] nums) { int[] t=new int[nums.length]; for(int i=0,j=0,k=1;i<nums.length;i++){ if(nums[i]%2==0){ t[j]=nums[i]; j+=2; }else{ t[k]=nums[i]; k+=2; } } return t; ...
922. Sort Array By Parity II/** * 922. Sort Array By Parity II *https://leetcode.com/problems/sort-array-by-parity-ii/description/ * * Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that ...
leetCode/Array/SortArrayByParityII.py/ Jump to 47 lines (32 sloc)997 Bytes RawBlame """ Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i...
def sortArrayByParity(self, nums: List[int]) -> List[int]: ans = [0] * len(nums) i, j = 0, -1 for x in nums: if x % 2: ans[j] = x j -= 1 else: ans[i] = x i += 1 return ans #return sorted(nums, key=lambda x : x % 2) ...