LintCode "Partition Array by Odd and Even"

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)
        {
            int v = nums[i];
            if (v & 0x1) // odd
            {
                swap(nums[i], nums[io++]);
            }
            else    // even
            {
                swap(nums[i], nums[ie--]);
            }
            i = io;
        }
    }
};
时间: 2024-11-15 10:45:13

LintCode "Partition Array by Odd and Even"的相关文章

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.注意判断奇偶时,利用位运

lintcode-easy-Partition Array by Odd and Even

Partition an integers array into odd number first and even number second. Given [1, 2, 3, 4], return [1, 3, 2, 4] public class Solution { /** * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { // write

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】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

双指针 — 20180928 - 20181012

同向双指针: 100. Remove Duplicates from Sorted Array 1 public class Solution { 2 /* 3 * @param nums: An ineger array 4 * @return: An integer 5 */ 6 public int removeDuplicates( int [] nums) { 7 // write your code here 8 9 if (nums == null ||nums.length ==