LeetCode——905. 按奇偶排序数组

简介: LeetCode——905. 按奇偶排序数组

905. 按奇偶排序数组


题目描述

答案

我的答案

官方答案

方法一:两次遍历

方法二:双指针 + 一次遍历

方法三:原地交换


题目描述


给你一个整数数组 nums,将 nums 中的的所有偶数元素移动到数组的前面,后跟所有奇数元素。


返回满足此条件的 任一数组 作为答案。


示例 1:

输入:nums = [3,1,2,4]

输出:[2,4,3,1]

解释:[4,2,3,1]、[2,4,1,3] 和 [4,2,1,3] 也会被视作正确答案。


示例 2:

输入:nums = [0]

输出:[0]


提示:

1 <= nums.length <= 5000

0 <= nums[i] <= 5000


答案


我的答案


class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int[] arr = new int[nums.length];
        int left = 0;
        int right = nums.length-1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]%2==0){
                arr[left++] = nums[i];
            }else {
                arr[right--] = nums[i];
            }
        }
        return arr;
    }
}


官方答案


方法一:两次遍历


思路

新建一个数组 res 用来保存排序完毕的数组。遍历两次 nums,第一次遍历时把所有偶数依次追加到 res 中,第二次遍历时把所有奇数依次追加到 res 中。

代码

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length, index = 0;
        int[] res = new int[n];
        for (int num : nums) {
            if (num % 2 == 0) {
                res[index++] = num;
            }
        }
        for (int num : nums) {
            if (num % 2 == 1) {
                res[index++] = num;
            }
        }
        return res;
    }
}


复杂度分析


时间复杂度:O(n),其中 n 为数组 nums 的长度。需遍历 nums 两次次。


空间复杂度:O(1)。结果不计入空间复杂度。


方法二:双指针 + 一次遍历


思路


记数组 nums 的长度为 n。方法一需要遍历两次 nums,第一次遍历时遇到奇数会跳过,第二次遍历时遇到偶数会跳过,这部分可以优化。


新建一个长度为 n 的数组 res 用来保存排完序的数组。遍历一遍 nums,遇到偶数则从 res 左侧开始替换元素,遇到奇数则从 res 右侧开始替换元素。遍历完成后,res 就保存了排序完毕的数组。

代码

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        int left = 0, right = n - 1;
        for (int num : nums) {
            if (num % 2 == 0) {
                res[left++] = num;
            } else {
                res[right--] = num;
            }
        }
        return res;
    }
}


复杂度分析


时间复杂度:O(n),其中 nn 为数组 nums 的长度。只需遍历 nums 一次。


空间复杂度:O(1)。结果不计入空间复杂度。


方法三:原地交换


思路


记数组 nums 的长度为 n。先从 nums 左侧开始遍历,如果遇到的是偶数,就表示这个元素已经排好序了,继续从左往右遍历,直到遇到一个奇数。然后从 nums 右侧开始遍历,如果遇到的是奇数,就表示这个元素已经排好序了,继续从右往左遍历,直到遇到一个偶数。交换这个奇数和偶数的位置,并且重复两边的遍历,直到在中间相遇,nums 排序完毕。

代码

class Solution {
    public int[] sortArrayByParity(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 == 1) {
                right--;
            }
            if (left < right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
            }
        }
        return nums;
    }
}


复杂度分析


时间复杂度:O(n)。原数组中每个元素只遍历一次。

空间复杂度:O(1)。原地排序,只消耗常数空间。

相关文章
|
3天前
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
【Leetcode】两数之和,给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
|
3天前
|
算法
leetcode代码记录(寻找两个正序数组的中位数
leetcode代码记录(寻找两个正序数组的中位数
13 2
|
3天前
|
索引
leetcode代码记录(最长重复子数组
leetcode代码记录(最长重复子数组
12 0
|
3天前
leetcode代码记录(两个数组的交集
leetcode代码记录(两个数组的交集
9 1
|
3天前
leetcode代码记录(最大子数组和
leetcode代码记录(最大子数组和
11 2
|
3天前
|
存储 算法
Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
给定一个无序整数数组和目标值,找出数组中和为目标值的两个数的下标。要求不重复且可按任意顺序返回。示例:输入nums = [2,7,11,15], target = 9,输出[0,1]。暴力解法时间复杂度O(n²),优化解法利用哈希表实现,时间复杂度O(n)。
22 0
|
3天前
|
索引
Leetcode 给定一个数组,给定一个数字。返回数组中可以相加得到指定数字的两个索引
Leetcode 给定一个数组,给定一个数字。返回数组中可以相加得到指定数字的两个索引
|
3天前
【力扣】238. 除自身以外数组的乘积
【力扣】238. 除自身以外数组的乘积
|
3天前
|
C++
【力扣】2562. 找出数组的串联值
【力扣】2562. 找出数组的串联值
|
3天前
|
算法 C++ 索引
【力扣经典面试题】238. 除自身以外数组的乘积
【力扣经典面试题】238. 除自身以外数组的乘积