lintcode-Array Partitioning

Source: Internet
Author: User

Title Description:

Gives an array of integers nums and an integer k. Dividing an array (that is, moving an element in an array nums) makes:

      • All elements less than k are moved to the left
      • All elements greater than or equal to K are moved to the right

Returns the position of the array, that is, the first position in the array I, satisfies nums[i] is greater than or equal to K.

Precautions

You should really divide the array nums, not just the number of integers smaller than k, if all the elements in the array nums are smaller than K, the Nums.length is returned.

Sample Example

Give the array nums=[3,2,2,1] and k=2, return 1

1  Public classSolution {2     /** 3      *@paramnums:the integer array you should partition4      *@paramK:as Description5 *return:the Index after partition6      */7      Public intPartitionarray (int[] Nums,intk) {8        intLength =nums.length;9           for(inti=0;i<length;i++){Ten              if(nums[i]>=k) { One                   for(intj=length-1;j>=i;j--){ A                      if(nums[j]<k) { -                          inttemp; -temp =Nums[i]; theNums[i] =Nums[j]; -NUMS[J] =temp; -Length = j+1; -                           Break; +                      } -                      if(j==i) { +                          returnJ; A                      } at                  } -              } -          } -          returnnums.length; -     } -}

lintcode-Array Partitioning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.