TreeviewCopyright © aleen42 all right reserved, powered by aleen42

21. 调整数组顺序使奇数位于偶数前面

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

Java

class Solution {

    // 双指针法
    public int[] exchange(int[] nums) {
        // 初始化头尾指针
        int left = 0, right = nums.length - 1;
        // 通过移动头尾指针来循环遍历数组元素
        while(left < right) {
            // 若当前元素为奇数则持续从左往右移动头指针,直到定位到偶数元素
            while(left < right && nums[left] % 2 != 0) left++;
            // 若当前元素为偶数则持续从右往左移动尾指针,直到定位到奇数元素
            while(left < right && nums[right] % 2 == 0) right--;
            // 交换奇偶元素的位置
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }
        // 返回结果
        return nums;
    }
}

Python

class Solution(object):
    def exchange(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        i = 0
        # 循环遍历数组元素
        for j in range(len(nums)):
            # 若当前元素为奇数,则交换奇偶元素的位置
            if nums[j] % 2 != 0:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1
        # 返回结果
        return nums
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""