欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

剑指offer_数组_调整数组顺序使奇数位于偶数前面

程序员文章站 2022-07-10 12:20:02
...

数组_调整数组顺序使奇数位于偶数前面

剑指offer_数组_调整数组顺序使奇数位于偶数前面
思路:可参考https://blog.csdn.net/qq_33487726/article/details/92790140
https://blog.csdn.net/qq_20141867/article/details/81105004

# -*- coding:utf-8 -*-
class Solution:
    def reOrderArray(self, array):
        # write code here
        num1 = 0
        num0 = 0
        length = len(array)
        for e in array:
            if e%2 == 1:
                num1 += 1
        res = [None]*length
        for e in array:
            if e % 2 == 1:
                res[num0] = e
                num0 += 1
            else:
                res[num1] = e
                num1 += 1
        return res