LyWang

partition

Nov 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. class Solution:
  2.     """
  3.    @param nums: The integer array you should partition
  4.    @param k: An integer
  5.    @return: The index after partition
  6.    """
  7.     def partitionArray(self, nums, k):
  8.         # write your code here
  9.         if len(nums) == 0:
  10.             return 0
  11.         j = len(nums)-1
  12.         i = 0
  13.         while i <=j and nums[i]<k:
  14.             i+=1
  15.         if i > j:
  16.             return len(nums)
  17.         while nums[j] >= k:
  18.             if j > i:
  19.                 j -= 1
  20.             else:
  21.                 break
  22.         if j == i:
  23.             return j
  24.         while i<j:
  25.             if nums[i]>=k:
  26.                 while nums[j]>=k:
  27.                     if j > i:
  28.                         j-=1
  29.                     else:
  30.                         return j
  31.                 temp = nums[j]
  32.                 nums[j]=nums[i]
  33.                 nums[i]=temp
  34.             i+=1
  35.         return j
Add Comment
Please, Sign In to add comment