剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
//双指针法,left在前找偶数,right在后找奇数,交换。继续找,当俩个指针碰面,跳出 class Solution { public int[] exchange(int[] nums) { int left = 0, right = nums.length - 1; while(left < right){ //left找偶数 while(left < right && nums[left] % 2 != 0){ left++; } //right找奇数 while(left < right && nums[right] % 2 == 0){ right--; } //交换位置 int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; } return nums; } }