题解 | #调整数组顺序使奇数位于偶数前面(二)#
调整数组顺序使奇数位于偶数前面(二)
http://www.nowcoder.com/practice/0c1b486d987b4269b398fee374584fc8
#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param array int整型一维数组
# @return int整型一维数组
#
class Solution:
def reOrderArrayTwo(self , array ):
# write code here
leftCursor = 0
rightCursor = len(array) -1
while leftCursor < rightCursor:
while array[rightCursor] %2 ==0:
rightCursor -= 1
if rightCursor < 0:
break
continue
while array[leftCursor] %2 == 1:
leftCursor += 1
if leftCursor > len(array)-1:
break
continue
if rightCursor <= leftCursor:
break
array[leftCursor], array[rightCursor] = array[rightCursor], array[leftCursor]
# print(array)
return array
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param array int整型一维数组
# @return int整型一维数组
#
class Solution:
def reOrderArrayTwo(self , array ):
# write code here
leftCursor = 0
rightCursor = len(array) -1
while leftCursor < rightCursor:
while array[rightCursor] %2 ==0:
rightCursor -= 1
if rightCursor < 0:
break
continue
while array[leftCursor] %2 == 1:
leftCursor += 1
if leftCursor > len(array)-1:
break
continue
if rightCursor <= leftCursor:
break
array[leftCursor], array[rightCursor] = array[rightCursor], array[leftCursor]
# print(array)
return array