LintCode-Partition Array

Given an array "nums" of integers and an int "k", Partition the array (i.e move the elements in "nums") such that,

* All elements < k are moved to the left

* All elements >= k are moved to the right

Return the partitioning Index, i.e the first index "i" nums[i] >= k.

Note

You should do really partition in array "nums" instead of just counting the numbers of integers smaller than k.

If all elements in "nums" are smaller than k, then return "nums.length"

Example

If nums=[3,2,2,1] and k=2, a valid answer is 1.

Challenge

Can you partition the array in-place and in O(n)?

Solution:

 1 public class Solution {
 2     /**
 3      *@param nums: The integer array you should partition
 4      *@param k: As description
 5      *return: The index after partition
 6      */
 7     public int partitionArray(ArrayList<Integer> nums, int k) {
 8         //if (nums.isEmpty()) return 0;
 9         int len = nums.size();
10         if (len==0) return 0;
11
12         int p1 = 0, p2 = len-1;
13         while (p1<len && nums.get(p1)<k) p1++;
14         while (p2>=0 && nums.get(p2)>=k) p2--;
15
16         while (p1<p2){
17             //swap the element at p1 and p2.
18             int temp = nums.get(p1);
19             nums.set(p1,nums.get(p2));
20             nums.set(p2,temp);
21
22             //Move p1 and p2.
23             p1++;
24             while (p1<len && nums.get(p1)<k) p1++;
25             p2--;
26             while (p2>=0 && nums.get(p2)>=k) p2--;
27         }
28
29         return p1;
30     }
31 }
时间: 2024-10-14 10:06:49

LintCode-Partition Array的相关文章

LintCode &quot;Partition Array by Odd and Even&quot;

One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integers * @return: nothing */ void partitionArray(vector<int> &nums) { size_t len = nums.size(); int i = 0, io = 0, ie = len - 1; while (io < ie)

Lintcode31 Partition Array solution题解

[题目描述] Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:All elements < k are moved to the left;All elements >= k are moved to the right;Return the partitioning index, i.e the fir

Partition Array

Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that: All elements < k are moved to the left All elements >= k are moved to the right Return the partitioning index, i.e the first ind

leetcode:Partition Array by odd and even

1. Partition an integers array into odd number first and even number second. Given [1, 2, 3, 4], return [1, 3, 2, 4] 2.思路 1.通过两次遍历,不合算. 2.一次遍历,一个从头,一个从尾,如果碰到偶数,兑换位置,此方法为排序. 3. public void partitionArray(int[] nums) { int start = 0, end = nums.length

Partition Array by Odd and Even

Partition an integers array into odd number first and even number second. 剑指offer的一道题,把所有奇数移动到偶数前面,其实是partition的双端解法,利用双指针.先检测两边合格的元素,都不合格,则交换,继续. 需要注意的是: 1.移动时,防止全部是偶数或者全部是奇数的情况,防止移动时越界. 2.交换时,仍然需要防止越界,全奇数或者全偶数,则left== right, 此时不应该交换. 3.注意判断奇偶时,利用位运

【leetcode】1043. Partition Array for Maximum Sum

题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given arr

(Easy) Partition Array Into Three Parts With Equal Sum - LeetCode

Description: Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums. Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1]

[Lintcode] Partition List

Partition List Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道.在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单.要求找到2个数组的交集,简单点的方法就是用2个hashSet,第一个HashSet存第一个数组的元素.然后扫描第二个数组,如果第二个数组中的元素在第一个HashSet中出现了,那么就把它加到第二个HashSet中.最后第二个HashSet就是两个数组的交集了. 2)138. Subarray Sum 要求在一个数组中找到一个子数

LintCode Problems Link Table

Practice Makes Perfect! # Problem Link Tag Difficulty 1 A + B problem Bitwise Operation Easy 2 Trailing Zeros Math Easy 3 Digit Counts   Medium 4 Ugly Number II   Medium 5 Kth Largest Element   Medium 6 Merge Two Sorted Arrays   Easy 7 Binary Tree Se