Can you solve this real interview question? Sort Array By Parity - Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers. Return any array that satisfies this condition. Example 1: I
public int[] sortArrayByParityII(int[] A) { LinkedList<Integer> list = new LinkedList<Integer>(); for (int i = 0; i < A.length; i++) list.add(A[i]);//list.add()返回的是boolean类型 即是否添加成功 for(int i=0;i<list.size();i++) System.out.print(list.get(i)+" "); ...
此解法的时间复杂度是O(N),空间复杂度是O(1)。 publicint[] sortArrayByParityII4(int[] A) {inti=0, j = A.length-1, n = A.length;while(i < n && j >=1) {if(A[i]%2==1&& A[j]%2==0) {inttem=A[j]; A[j] = A[i]; A[i] = tem; }if(A[i]%2==0) { i +=2...
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 array that satisfies this condition. Exampl...
public int[] sortArrayByParity(int[] A) { int st = 0; int ed = A.length - 1; while (st < ed) { while (st < A.length && A[st] % 2 == 0) st++; while (ed >= 0 && A[ed] % 2 == 1) ed--; if (st >= ed) break; ...
leetcode 922. Sort Array By Parity II ... LeetCode #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; an......
Sort Array By Parity LeetCode: 905. Sort Array By Parity 题目描述 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 ......
代码# Go packageleetcodefuncsortArrayByParityII(A[]int)[]int{iflen(A)==0||len(A)%2!=0{return[]int{}}res:=make([]int,len(A))oddIndex:=1evenIndex:=0fori:=0;i<len(A);i++{ifA[i]%2==0{res[evenIndex]=A[i]evenIndex+=2}else{res[oddIndex]=A[i]oddIndex+=2}}returnres...
Sort the values at odd indices of nums in non-increasing order. For example, if nums = [4,1,2,3] before this step, it becomes [4,3,2,1] after. The values at odd indices 1 and 3 are sorted in non-increasing order. Sort the values at even indices of nums in non-decreasing ...
:pencil2: 算法相关知识储备 LeetCode with Python :books:. Contribute to HuberTRoy/leetCode development by creating an account on GitHub.