lintcode 373. 奇偶分割数组

2018-09-07  本文已影响9人  cuizixin

难度:容易

1. Description

373. 奇偶分割数组

2. Solution

class Solution:
    """
    @param: nums: an array of integers
    @return: nothing
    """
    def partitionArray(self, nums):
        # write your code here
        n = len(nums)
        if n<=1:
            return nums
        left = 0
        right = n-1
        while True:
            while nums[left]%2!=0 and left<n-1: # find even num
                left+=1
            while nums[right]%2==0 and right>0: # find odd num
                right-=1
            if left<right:
                nums[left],nums[right]=nums[right],nums[left]
            else:
                break
        return nums

3. Reference

  1. https://www.lintcode.com/problem/partition-array-by-odd-and-even/description?_from=ladder
上一篇下一篇

猜你喜欢

热点阅读