classSolution{public:vector<int>sortArrayByParityII(vector<int>& nums){intn=nums.size();intodd=1;for(inti=0;i<n;i+=2){if(nums[i]%2==1){//偶数位发现奇数while(nums[odd]%2==1){//目的是要在奇数位找到偶数odd+=2;}swap(nums[i],nums[odd]);}}returnnums;}}; __EOF__ 关于博主...
} public void swap(int [] Array,int i,int j){ int temp=Array[i]; Array[i]=Array[j]; Array[j]=temp; }
[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......
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...
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; ...
LeetCode-922. Sort Array By Parity II i++文章分类运维 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; ...
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...
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 ...
def sortArrayByParityII(self, nums: List[int]) -> List[int]: ''' n, i, j = len(nums), 0, 1 ans = [0] * n for x in nums: if x % 2: ans[j] = x j += 2 else: ans[i] = x i += 2 return ans ''' n, i, j = len(nums), 0, 1 ...
922. Sort Array By Parity II 题解 280. Wiggle Sort 题解 324. Wiggle Sort II 题解 1054. Distant Barcodes 题解 767. Reorganize String 题解 969. Pancake Sorting 题解编辑于 2019-08-14 11:24 算法与数据结构 排序算法 力扣(LeetCode) ...