LintCode:数组划分

LintCode:数组划分

一前一后两根指针,当前面的指针大于k且后指针小于k时,交换两个指针的值,走到两根指针相遇。

class Solution:
    """ @param nums: The integer array you should partition @param k: As description @return: The index after partition """
    def partitionArray(self, nums, k):
        # write your code here
        # you should partition the nums by k
        # and return the partition index as description
        m = 0
        n = len(nums) - 1
        while(m < n):
            while nums[m] < k and m < n:
                m += 1
            while nums[n] >= k and m < n:
                n -= 1
            nums[m], nums[n] = nums[n], nums[m]
            m = m + 1
            n = n - 1
        for i in range(len(nums)):
            if nums[i] >= k:
                return i
        return len(nums)

你可能感兴趣的:(LintCode:数组划分)