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 your code here;

        if(nums == null || nums.length <= 1)
            return;

        int left = 0;
        int right = nums.length - 1;

        while(true){
            while(left < right && nums[left] % 2 != 0)
                left++;
            while(left < right && nums[right] % 2 == 0)
                right--;

            if(left == right)
                break;
            swap(nums, left, right);
        }

        return;
    }

    public void swap(int[] nums, int i, int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;

        return;
    }

}
时间: 2024-10-25 05:17:15

lintcode-easy-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 &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)

(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 easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver

[lintcode easy]Merge Sorted Array

Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Have you met this question in a real interview? Yes Which company asked you this question? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 Note There may exist multiple valid solutions, return any of them. ////////////////////// 二叉查

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