牛客网在线编程专题《剑指offer-面试题14》调整数组顺序使奇数位于偶数前面

微信公众号

题目链接:

https://www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

题目描述:

解题思路:

(1)使用空间换取时间效率

最简单的办法就是利用Python的优点,不用排序直接将奇数和偶数拿出来存放,再合并到一起。

class Solution:

    def reOrderArray(self, array):
        odd = []
        even = []
        for i in range(len(array)):
            if array[i] & 0x1 == 1:
                odd.append(array[i])
            else:
                even.append(array[i])

        return odd + even

sol = Solution()
# array = [1,4,7,2,9,1,4,7,3,5,11]
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(sol.reOrderArray(array))

(2)不使用额外空间

如果要求不能使用额外的空间,那么可以借鉴冒泡排序的思想。如果当前数为偶数,后一个数为奇数,则相互交换。

class Solution:
    def reOrderArray(self, array):
        _len = len(array)
        for i in range(_len-1):
            if (array[i] & 0x1 == 0)and (array[i+1] & 0x1 == 1):
                temp = array[i]
                array[i] = array[i+1]
                array[i+1] = temp
                for j in range(i, 0, -1):
                    if (array[j] & 0x1 == 1)and (array[j-1] & 0x1 == 0):
                        temp = array[j]
                        array[j] = array[j-1]
                        array[j-1] = temp
        return array

sol = Solution()
# array = [0,1,2,3,4,5,6,7,8,9]
array = [1,4,7,2,9,1,4,7,3,5,11]
print(sol.reOrderArray(array))

优化后的代码:

class Solution:
    def reOrderArray(self, array):
        for i in range(len(array)):
            for j in range(len(array)-1):
                if array[j] % 2 == 0 and array[j+1] % 2 == 1:
                    temp = array[j+1]
                    array[j+1] = array[j]
                    array[j] = temp
        return array

sol = Solution()
array = [0,1,2,3,4,5,6,7,8,9]
print(sol.reOrderArray(array))

Reference:

【1】https://www.jianshu.com/p/ed83c477b6a7

【2】https://suixinblog.cn/2019/03/target-offer-reorder-array.html

发布了285 篇原创文章 · 获赞 892 · 访问量 111万+

猜你喜欢

转载自blog.csdn.net/program_developer/article/details/97321022