题目
教练使用整数数组 actions 记录一系列核心肌群训练项目编号。为增强训练趣味性,需要将所有奇数编号训练项目调整至偶数编号训练项目之前。请将调整后的训练项目编号以 数组 形式返回。
示例 1:
输入:actions = [1,2,3,4,5]
输出:[1,3,5,2,4]
解释:为正确答案之一
提示:
0 <= actions.length <= 50000
0 <= actions[i] <= 10000
代码
class Solution {
public int[] trainingPlan(int[] nums) {
if(nums == null || nums.length == 0){
return nums;
}
int left = 0;
int right = nums.length - 1;
while(left < right){
while(left < right && nums[left] % 2 != 0) left++;
while(left < right && nums[right] % 2 != 1) right–;
int temp = nums[left];nums[left] = nums[right];nums[right] = temp;}return nums;
}
}
时间复杂度:O(n)
额外空间复杂度:O(1)