剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
地址:剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
问题描述:
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。
示例:
输入:nums = [1,2,3,4]
输出:[1,3,2,4]
注:[3,1,2,4] 也是正确的答案之一。提示:
1 <= nums.length <= 50000
1 <= nums[i] <= 10000
//快排思想应用
object Solution {
def exchange(nums: Array[Int]): Array[Int] = {
var (left, right) = (0, nums.length-1)
while (left < right) {
//left += 1
while(left < right && (nums(left)&1) == 1){
left += 1
}
//right -= 1
while(left < right && (nums(right)&1) == 0){
right -= 1
}
if (left < right) {
val temp = nums(left)
nums(left) = nums(right)
nums(right) = temp
}
println("left: " + left)
println("right: " + right)
}
return nums
}
}
//快慢指针方法
import "fmt"
func exchange(nums []int) []int {
fast ,slow := 0, 0
for fast < len(nums) {
if (nums[fast] & 1) == 1 {
temp := nums[fast]
nums[fast] = nums[slow]
nums[slow] = temp
slow += 1
}
//fast += 1
//fmt.Println("-------------------")
//fmt.Printf("fast: %d
", fast)
//fmt.Printf("slow: %d
", slow)
//fmt.Println(nums)
fast += 1
}
return nums
}