美文网首页
lintcode 373. 奇偶分割数组

lintcode 373. 奇偶分割数组

作者: cuizixin | 来源:发表于2018-09-07 01:43 被阅读9次

    难度:容易

    1. Description

    373. 奇偶分割数组

    2. Solution

    • python
    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

    相关文章

      网友评论

          本文标题:lintcode 373. 奇偶分割数组

          本文链接:https://www.haomeiwen.com/subject/uzkqgftx.html