您的位置:首页 > 其它

LintCode:数组划分

2016-04-26 23:39 253 查看
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
>= k and m < n:
n -= 1
nums[m], nums
= nums
, nums[m]
m = m + 1
n = n - 1
for i in range(len(nums)):
if nums[i] >= k:
return i
return len(nums)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: