快速排序中的一次partition習題

一:lintCode 31. 數組劃分

題意:
給出一個整數數組 nums 和一個整數 k。劃分數組(即移動數組 nums 中的元素),使得:
所有小於k的元素移到左邊
所有大於等於k的元素移到右邊
返回數組劃分的位置,即數組中第一個位置 i,滿足 nums[i] 大於等於 k。

python實現:

def partitionArray(self, nums, k):
        # write your code here
        if not nums: return 0
        
        def partition(nums, k):
            key = nums[0]
            i, j = 0, len(nums)-1
            
            while i < j:
                while i < j and nums[j] >= k: j -= 1  # 所有小於k的元素移到左邊
                if i >= j: break
                nums[i] = nums[j]
                i += 1
                while i < j and nums[i] < k: i += 1 # 所有大於等於k的元素移到右邊
                if i >= j: break
                nums[j] = nums[i]
                j -= 1
            
            nums[i] = key
            if nums[i] < k: return i+1  # 支點還是小
            return i
        
        return partition(nums, k)

二:lintCode 373. 奇偶分割數組

分割一個整數數組,使得奇數在前偶數在後。

python實現:

	def partitionArray(self, nums):
        # write your code here
        if not nums: return []
        
        i, j = 0, len(nums)-1
        key = nums[0]
        
        while i < j:
            while i < j and nums[j] % 2 == 0: j -= 1
            if i >= j: break
            nums[i] = nums[j]
            i += 1
            while i < j and nums[i] % 2 == 1: i += 1
            if i >= j: break
            nums[j] = nums[i]
            j -= 1
        
        nums[i] = key

lintCode 144. 交錯正負數

題意:給出一個含有正整數和負整數的數組,重新排列成一個正負數交錯的數組。

實現:

  • 注意正負總數不同的情況
  • 採用2個遊標分別指向偶數下標和奇數下標,交換不符合條件的元素
def rerange(self, A):
        # write your code here
        
        tag = 0 # 看正負數的個數, tag > 0, 負數多
        for a in A:
            if a > 0: tag -= 1
            else: tag += 1
        
        if not tag: tag = 1 # tag = 0表示正負數一樣多
        i = 0
        j = 1
        
        while i < len(A) and j < len(A):
            while i < len(A) and tag * A[i] < 0: i += 2
            while j < len(A) and tag * A[j] > 0: j += 2
            if i < len(A) and j < len(A):
                A[i], A[j] = A[j], A[i]
            i += 2
            j += 2

215. Kth Largest Element in an Array

尋找數組中第K大的元素

class Solution(object):
    def findKthLargest(self, nums, k):
        def partition(nums, left, right): # 從大到小啊
            key = nums[left]
            while left < right:
                while left < right and nums[right] <= key: right -= 1
                if left >= right: break
                nums[left] = nums[right]
                left += 1
                while left < right and nums[left] >= key: left += 1
                if left >= right: break
                nums[right] = nums[left]
                right -= 1
            
            nums[left] = key
            return left
        
        # 下面就是進行K個劃分,第K次的支點就是第K大的數
        left, right = 0, len(nums)-1
        while True:
            pivot = partition(nums, left, right)  # pivot的下標,也就是找到第幾大的數

            if pivot < k-1:   # 第k大元素必出現在右子數組
                left = pivot + 1
            elif pivot > k-1:  # 第k大元素必出現在左子數組
                right = pivot - 1
            else:
                return nums[pivot]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章