LintCode Q373 Partition Array by Odd and Even in Python

  • Jinhai ZHOU
  • 2 Minutes
  • 2017年2月7日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
# @param nums: a list of integers
# @return: nothing
def partitionArray(self, nums):
# write your code here
odd_ptr = 0
even_ptr = len(nums) - 1
while odd_ptr < even_ptr:
while odd_ptr < even_ptr and self.isOdd(nums[odd_ptr]):
odd_ptr += 1
while odd_ptr < even_ptr and not self.isOdd(nums[even_ptr]):
even_ptr -= 1
nums[odd_ptr], nums[even_ptr] = nums[even_ptr], nums[odd_ptr]
def isOdd(self, num):
return num % 2 == 1
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。