剑指 Offer 第21题:调整数组顺序使奇数位于偶数前面
2022-07-11 本文已影响0人
放开那个BUG
1、前言

2、思路
3、代码
class Solution {
public int[] exchange(int[] nums) {
if(nums == null || nums.length == 0){
return new int[]{};
}
int left = 0, right = nums.length - 1;
while(left < right){
if(nums[left] % 2 != 0){
left++;
}else {
swap(nums, left, right--);
}
}
return nums;
}
private void swap(int[] nums, int left, int right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}