373.Partition Array by Odd and Even-奇偶分割数组(容易题)
来源:互联网 发布:拳皇2002um键盘优化 编辑:程序博客网 时间:2024/06/02 07:08
奇偶分割数组
题目
分割一个整数数组,使得奇数在前偶数在后。
样例
给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]。
挑战
在原数组中完成,不使用额外空间。
题解
首尾双指针遍历,start指针查找偶数,end指针查找奇数,然后进行交换。
public class Solution { /** * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { int start = 0; int end = nums.length-1; while (start < end) { while ((nums[start] & 1) == 1) { start++; } while ((nums[end] & 1) == 0) { end--; } if(start < end) { nums[start]=nums[start]+nums[end]; nums[end]=nums[start]-nums[end]; nums[start]=nums[start]-nums[end]; } } }}
Last Update 2016.9.13
0 0
- 373.Partition Array by Odd and Even-奇偶分割数组(容易题)
- Partition Array by Odd and Even
- Partition Array by Odd and Even
- [Lintcode]Partition Array by Odd and Even
- Partition Array by Odd and Even
- #373 Partition Array by Odd and Even
- Partition Array by Odd and Even
- Partition Array by Odd and Even 解题报告
- LintCode on Array by Odd and Even
- [剑指offer][第三章][14]Partition Array by Odd and Even
- 奇偶排序Odd-even sort
- A even and odd(13.7.6)
- CodeForces--621A--Wet Shark and Odd and Even(数学水题)
- Wet Shark and Odd and Even (水题)
- [LintCode]373.奇偶分割数组
- 算法题:奇偶分割数组
- 奇偶分割数组(LintCode)
- UVa 12683 Odd and Even Zeroes(数论+数位DP)
- CentOS6.5 安装pip的方法
- @property属性详解
- 如何向通话的另一方号码发送dtmf Export原理和使用
- Oracle SQL turning
- Android studio binder 进程间通信
- 373.Partition Array by Odd and Even-奇偶分割数组(容易题)
- C#入门经典第6版学习 七
- Kinect V2 开发专题(3)设备信息获取与音频功能探索
- MySQL.zip配置及问题解决
- dos命令计算MD5与SHA1/256值
- AndFix 安卓的热修复
- 演示Set
- POJ 3233 Matrix Power Series [矩阵快速幂]【数论】[水]
- 作用域/上下文的理解,闭包的理解