Java&C++题解与拓展——leetcode905.按奇偶排序数组【么的新知识】

120 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

每日一题做题记录,参考官方和三叶的题解

题目要求

image.png

思路一:双指针+原地交换

【感觉之前做过什么类似题】

从头遍历,遇到奇数从尾巴找一个偶数对调,到指针相遇结束。

Java

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int l = 0, r = nums.length - 1;
        while(l < r) {
            while(l < r && nums[l] % 2 == 0) // 为偶数
                l++;
            while(l < r && nums[r] % 2 == 1) // 为奇数
                r--;
            // 左奇右偶需交换
            if(l < r) {
                int tmp = nums[l];
                nums[l] = nums[r];
                nums[r] = tmp;
                l++;
                r--;
            }
        }
        return nums;
    }
}
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:真丶O(1)O(1)

C++

遇到奇数就和后面换,不管后面是啥,多判断一次,要是换过来还是奇数就继续换。

P.S.顺便get到了一个方便的内置函数

【简单题的两种语言代码总是相似得像是在凑字数,所以就参考了三叶姐姐的另一种思路】

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        int n = nums.size();
        for(int l = 0, r = n - 1; l < r; l++) {
            if(nums[l] % 2 == 1) 
                swap(nums[l--], nums[r--]);
        }
        return nums;
    }
};
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:真丶O(1)O(1)

思路二:两次遍历

第一次遍历找到所有偶数,第二次找到奇数,放入结果。

Java

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int idx = 0; // 结果数组当前下标
        int[] res = new int[nums.length];
        for(int n : nums) // 找偶数
            if(n % 2 == 0)
                res[idx++] = n;
        for(int n : nums) // 找奇数
            if(n % 2 == 1)
                res[idx++] = n;
        return res;
    }
}
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1),算结果的话就是O(n)O(n)

C++

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        int idx = 0; // 结果数组当前下标
        vector<int> res(nums.size());
        for(int n : nums) // 找偶数
            if(n % 2 == 0)
                res[idx++] = n;
        for(int n : nums) // 找奇数
            if(n % 2 == 1)
                res[idx++] = n;
        return res;
    }
};
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1),算结果的话就是O(n)O(n)

思路三:一次遍历+双指针

上面遍历的时候跳过了奇数hin是浪费,所以搞个双指针指向结果的头尾,遍历遇到偶数放头、奇数放尾巴,一次遍历结束不浪费。

【其实是思路一的低配版】

Java

class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length;
        int[] res = new int[n];
        int l = 0, r = n - 1;
        for(int nu : nums) {
            if(nu % 2 == 0) // 偶、放头
                res[l++] = nu;
            else // 奇、放尾
                res[r--] = nu;
        }
        return res;
    }
}
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1),算结果的话就是O(n)O(n)

C++

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        int n = nums.size();
        vector<int> res(n);
        int l = 0, r = n - 1;
        for(int nu : nums) {
            if(nu % 2 == 0) // 偶、放头
                res[l++] = nu;
            else // 奇、放尾
                res[r--] = nu;
        }
        return res;
    }
};
  • 时间复杂度:O(n)O(n)
  • 空间复杂度:O(1)O(1),算结果的话就是O(n)O(n)

总结

简单题简单题~ 不是单纯模拟的简单题~

这两天搞论文好忙好忙……下个月还要软考都没时间好好摸鱼了嘤嘤嘤……


欢迎指正与讨论!