题目描述
- 有点像快排的交换操作
思路 & 代码
class Solution {public int[] exchange(int[] nums) {int left = 0, right = nums.length - 1;while(left < right){while(left < right && nums[left] % 2 == 1){left++;}while(left < right && nums[right] % 2 == 0){right--;}if(left < right){int temp = nums[left];nums[left] = nums[right];nums[right] = temp;}}return nums;}
}
二刷
class Solution {public int[] exchange(int[] nums) {int left = 0, right = nums.length - 1;while(left < right) {while(left < right && nums[right] % 2 == 0) {right--;}while(left < right && nums[left] % 2 == 1) {left++;}if(left < right) {int temp = nums[left];nums[left] = nums[right];nums[right] = temp;}}return nums;}
}