LintCode 373. Partition Array by Odd and Even

  • 2022-07-15 23:24:27

题目

LintCode 373. Partition Array by Odd and Even

思路

双指针,块排模板

代码

class Solution:
    """
    @param: nums: an array of integers
    @return: nothing
    """
    def partitionArray(self, nums):
        # write your code here
        i = 0; j = len(nums) - 1
        while i <= j:
            while i <= j and nums[i] % 2: i += 1
            while i <= j and not nums[j] % 2: j -= 1
            if i <= j:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1; j -= 1

猜你喜欢