题目
思路
双指针,交换数字。
代码
class Solution:
"""
@param nums: The integer array you should partition
@param k: An integer
@return: The index after partition
"""
def partitionArray(self, nums, k):
# write your code here
if not nums: return 0
i = 0; j = len(nums) - 1
while i < j:
while i < j and nums[i] < k:
i += 1
while i < j and nums[j] >= k:
j -= 1
if i < j:
nums[i], nums[j] = nums[j], nums[i]
if nums[i] < k: return i + 1
return i