LintCode:交错正负数

LintCode:交错正负数

class Solution:
    """
    @param A: An integer array.
    @return nothing
    """
    def rerange(self, A):
        # write your code here
        i = 0
        j = len(A) - 1
        while i < j:
            if A[i] > 0:
                while i < j and A[j] > 0:
                    j -= 1
                A[i], A[j] = A[j], A[i]
            i += 1
        mid = (len(A) - 1) / 2 
        if A[mid] < 0:
            if len(A) % 2 == 1:
                i = 1
                j = len(A) - 1
                while i < j:
                    A[i], A[j] = A[j], A[i]
                    i += 2
                    j -= 2
            else:
                i = 1
                j = len(A) - 2
                while i < j:
                    A[i], A[j] = A[j], A[i]
                    i += 2
                    j -= 2                

        else:
            i = 0
            j = len(A) - 2
            while i < j:
                A[i], A[j] = A[j], A[i]
                i += 2
                j -= 2

你可能感兴趣的:(lintcode,python)