剑指 Offer 第21题:调整数组顺序使奇数位于偶数前面
作者:
放开那个BUG | 来源:发表于
2022-07-11 15:48 被阅读0次
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;
}
}
本文标题:剑指 Offer 第21题:调整数组顺序使奇数位于偶数前面
本文链接:https://www.haomeiwen.com/subject/znmrurtx.html
网友评论