LeetCode 905. Sort Array By Parity

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

Example 1:

复制代码
Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Example 2:

复制代码
Input: nums = [0]
Output: [0]

Constraints:

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 5000

把一个数组里偶数放在前面,奇数放在后面。

自己的想法就是用two pointers分别指向前后,前面的如果遇到奇数就记下来,后面的如果遇到偶数就记下来,前后都有了以后就互相交换就完事了。于是就写了如下的代码,debug了巨久,各种corner case......感觉非常不优雅。

复制代码
class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int start = 0;
        int end = nums.length - 1;
        while (start < end) {
            while (start < nums.length && nums[start] % 2 == 0) {
                start++;
            }
            // now nums[start] is odd
            while (end >= 0 && nums[end] % 2 != 0) {
                end--;
            }
            // now nums[end] is even
            // swap nums[start] and nums[end]
            if (start < end) {
                int temp = nums[start];
                nums[start] = nums[end];
                nums[end] = temp;
                start++;
                end--;
            }
        }
        return nums;
    }
}

于是去solutions溜达了一圈,大家的想法都大差不差,但是很明显大家都不像我这么贪心,在while里还加while来想一次找到需要交换的start和end,而可以一步步来,这样会更清晰,也不用这么多corner case的条件。

复制代码
class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int start = 0;
        int end = nums.length - 1;
        while (start < end) {
            if (nums[start] % 2 == 0) {
                start++;
            } else {
                if (nums[end] % 2 == 0) {
                    int temp = nums[start];
                    nums[start] = nums[end];
                    nums[end] = temp;
                    start++;
                } 
                end--;
            }
        }
        return nums;
    }
}

后面又写到一道题的时候看了下也有人用我最开始写的while的方法,于是refine了一下,让它变得更优雅了,那我更喜欢这种做法了:

复制代码
class Solution {
    public int[] sortArrayByParity(int[] nums) {
        int start = 0;
        int end = nums.length - 1;
        while (start < end) {
            while (start < end && nums[start] % 2 == 0) {
                start++;
            }
            while (start < end && nums[end] % 2 != 0) {
                end--;
            }
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
        }
        return nums;
    }
}
相关推荐
Miraitowa_cheems5 分钟前
LeetCode算法日记 - Day 94: 最长的斐波那契子序列的长度
java·数据结构·算法·leetcode·深度优先·动态规划
ada7_11 分钟前
LeetCode(python)——49.字母异位词分组
java·python·leetcode
L_090713 分钟前
【Algorithm】Day-11
c++·算法·leetcode
一匹电信狗3 小时前
【C++】哈希表详解(开放定址法+哈希桶)
服务器·c++·leetcode·小程序·stl·哈希算法·散列表
nju_spy4 小时前
力扣每日一题(四)线段树 + 树状数组 + 差分
数据结构·python·算法·leetcode·面试·线段树·笔试
小年糕是糕手4 小时前
【数据结构】常见的排序算法 -- 插入排序
c语言·开发语言·数据结构·学习·算法·leetcode·排序算法
墨染点香5 小时前
LeetCode 刷题【142. 环形链表 II】
算法·leetcode·链表
海琴烟Sunshine5 小时前
leetcode 263. 丑数 python
python·算法·leetcode
User_芊芊君子5 小时前
【LeetCode 经典题解】:队列与栈的双向模拟——从原理到代码详解
linux·redis·leetcode
墨染点香9 小时前
LeetCode 刷题【144. 二叉树的前序遍历】
数据结构·算法·leetcode