Python程序在 O(n) 时间和 O(1) 额外空间内重新排列正数和负数
数组包含随机顺序的正数和负数。重新排列数组元素,以便交替放置正数和负数。正数和负数的个数不必相等。如果有更多的正数,它们会出现在数组的末尾。如果有更多的负数,它们也会出现在数组的末尾。
例如,如果输入数组是 [-1, 2, -3, 4, 5, 6, -7, 8, 9],那么输出应该是 [9, -7, 8, -3, 5, - 1、2、4、6]
注意:分区过程会改变元素的相对顺序。即,这种方法不保持元素出现的顺序。请参阅此内容以维护此问题中元素的出现顺序。
解决方法是先用 QuickSort 的分区过程将正数和负数分开。在分区过程中,将 0 视为枢轴元素的值,以便将所有负数放在正数之前。将负数和正数分开后,我们从第一个负数和第一个正数开始,并将每个交替的负数与下一个正数交换。
Python
# Python program to put positive numbers at even indexes (0, // 2, 4,..) and
# negative numbers at odd indexes (1, 3, 5, ..)
# The main function that rearranges elements of given array.
# It puts positive elements at even indexes (0, 2, ..) and
# negative numbers at odd indexes (1, 3, ..).
def rearrange(arr, n):
# The following few lines are similar to partition process
# of QuickSort. The idea is to consider 0 as pivot and
# divide the array around it.
i = -1
for j in range(n):
if (arr[j] < 0):
i += 1
# swapping of arr
arr[i], arr[j] = arr[j], arr[i]
# Now all positive numbers are at end and negative numbers
# at the beginning of array. Initialize indexes for starting
# point of positive and negative numbers to be swapped
pos, neg = i+1, 0
# Increment the negative index by 2 and positive index by 1,
# i.e., swap every alternate negative number with next
# positive number
while (pos < n and neg < pos and arr[neg] < 0):
# swapping of arr
arr[neg], arr[pos] = arr[pos], arr[neg]
pos += 1
neg += 2
# A utility function to print an array
def printArray(arr, n):
for i in range(n):
print arr[i],
# Driver program to test above functions
arr = [-1, 2, -3, 4, 5, 6, -7, 8, 9]
n = len(arr)
rearrange(arr, n)
printArray(arr, n)
# Contributed by Afzal
输出:
4 -3 5 -1 6 -7 2 8 9
时间复杂度: O(n),其中 n 是给定数组中的元素数。
辅助空间: O(1)
有关更多详细信息,请参阅有关在 O(n) 时间和 O(1) 额外空间中重新排列正负数的完整文章!