zoukankan      html  css  js  c++  java
  • 922. Sort Array By Parity II

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.

    Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.

    You may return any answer array that satisfies this condition.

    两个指针,一个是奇一个是偶,for一遍数组,是奇数就放到奇数指针的位置,偶数就放到偶数指针位置。

    class Solution(object):
        def sortArrayByParityII(self, A):
            """
            :type A: List[int]
            :rtype: List[int]
            """
            ans = [0] * len(A)
            even_index = 0
            odd_index = 1
            for value in A:
                if value % 2 == 0:
                    ans[even_index] = value
                    even_index += 2
                else:
                    ans[odd_index] = value
                    odd_index += 2
            return ans
  • 相关阅读:
    Wedding(2-SAT)
    JSOI2010 满汉全席
    2-SAT问题
    Tarjan求桥
    遥远的国度
    NOIP2014 联合权值
    部落冲突
    仓鼠找sugar
    2018.09.09 DL24 Day2总结
    php一些易犯的错误
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13207924.html
Copyright © 2011-2022 走看看