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.注意判断奇偶时,利用位运算 &0x1(python 1)也可以。可以加速。

代码如下:

class Solution:
    # @param nums: a list of integers
    # @return: nothing
    def partitionArray(self, nums):
        if not nums or len(nums) == 1:
            return
        left = 0
        right = len(nums) - 1
        while left < right:
            while left < right and nums[left] & 0x1 == 1:
                left += 1
            while left < right and nums[right] & 0x1 == 0:
                right -=1
            if left < right:
                nums[left],nums[right] = nums[right],nums[left]
                left += 1
                right -= 1

        return 
时间: 2024-11-13 01:00:51

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

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)

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]

双指针 — 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 ==

快速排序中的partition函数的枢纽元选择,代码细节,以及其标准实现

很多笔试面试都喜欢考察快排,叫你手写一个也不是啥事.我很早之前就学了这个,对快速排序的过程是很清楚的.但是最近自己尝试手写,发现之前对算法的细节把握不够精准,很多地方甚至只是大脑中的一个映像,而没有理解其真正的本质意图.于是今天结合了<数据结构>(严蔚敏),和<算法导论>进行一番探究. 首先先给出快速排序的严蔚敏版的实现(实际上这部分的partition也是算法导论里面思考题的实现方式,细节可能不一样): 1 public class QuickSort implements So