快速排序中的一次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]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值