Leetcode——905. Sort Array By Parity

17 篇文章 1 订阅
12 篇文章 0 订阅

Leetcode题目[1]:
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Note:

1 <= A.length <= 5000
0 <= A[i] <= 5000

题目解释:给定一个数组,将偶数的元素排在前面,奇数的元素排在后面。
解法一:
思路:将数组元组逐个元素判断奇偶性,然后偶性元素排在前面,奇数元素排在后面。

class Solution:
    def sortArrayByParity(self, A):
        """
        :type A: List[int]
        :rtype: List[int]
        """
        odd_result = []
        even_result = []
        results = []
        for i in A:
            if i%2 == 0:
                even_result.append(i)
            if i%2 == 1:
                odd_result.append(i)
        for i in even_result:
            results.append(i)
        for i in odd_result:
            results.append(i)
        return results

解法二[2]:

class Solution(object):
    def sortArrayByParity(self, A):
        A.sort(key = lambda x: x % 2)
        return A
Complexity Analysis

    Time Complexity: O(NlogN), where N is the length of A.

    Space Complexity: O(N) for the sort, depending on the built-in implementation of sort. 

解法三、

class Solution(object):
    def sortArrayByParity(self, A):
        return ([x for x in A if x % 2 == 0] +
                [x for x in A if x % 2 == 1])
Complexity Analysis

    Time Complexity: O(N), where N is the length of A.

    Space Complexity: O(N) for the sort, depending on the built-in implementation of sort. 

解法三、

Approach 3: In-Place

Intuition

If we want to do the sort in-place, we can use quicksort, a standard textbook algorithm.

Algorithm

We'll maintain two pointers i and j. The loop invariant is everything below i has parity 0 (ie. A[k] % 2 == 0 when k < i), and everything above j has parity 1.

Then, there are 4 cases for (A[i] % 2, A[j] % 2):

    If it is (0, 1), then everything is correct: i++ and j--.

    If it is (1, 0), we swap them so they are correct, then continue.

    If it is (0, 0), only the i place is correct, so we i++ and continue.

    If it is (1, 1), only the j place is correct, so we j-- and continue.

Throughout all 4 cases, the loop invariant is maintained, and j-i is getting smaller. So eventually we will be done with the array sorted as desired.
class Solution(object):
    def sortArrayByParity(self, A):
        i, j = 0, len(A) - 1
        while i < j:
            if A[i] % 2 > A[j] % 2:
                A[i], A[j] = A[j], A[i]

            if A[i] % 2 == 0: i += 1
            if A[j] % 2 == 1: j -= 1

        return A

复杂度分析:

Complexity Analysis

    Time Complexity: O(N), where N is the length of A. Each step of the while loop makes j-i decrease by at least one. (Note that while quicksort is O(NlogN) normally, this is O(N) because we only need one pass to sort the elements.)

    Space Complexity: O(1) in additional space complexity. 

Ref:
1、https://leetcode.com/problems/sort-array-by-parity/description/
2、https://leetcode.com/problems/sort-array-by-parity/solution/#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值