奇数偶数调整

题目

https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int[] exchange(int[] nums) {
int len = nums.length;
int left = 0, right = len -1;

while (left < right) {

while(nums[left]%2!=0 && left<right) {
left++;
}

while(nums[right]%2==0 && left<right) {
right--;
}
if (left < right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
}
return nums;
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×