[Lintcode]Partition Array by Odd and Even
来源:互联网 发布:奔奔数码淘宝王奔宏 编辑:程序博客网 时间:2024/06/17 22:31
Partition an integers array into odd number first and even number second.
Example
Given [1, 2, 3, 4]
, return [1, 3, 2, 4]
Challenge
双指针,一个指针从左至右寻找偶数,另一个从右至左寻找基数。找到之后互换。Do it in-place.
public class Solution { /** * @param nums: an array of integers * @return: nothing */ public void partitionArray(int[] nums) { int posOdd = 0, posEven = nums.length - 1; while(true) { while(posOdd < nums.length && nums[posOdd] % 2 == 1) posOdd++; while(posEven >= 0 && nums[posEven] % 2 == 0) posEven--; if(posOdd >= posEven) return; else { int tmp = nums[posEven]; nums[posEven] = nums[posOdd]; nums[posOdd] = tmp; } } }}
0 0
- [Lintcode]Partition Array by Odd and Even
- Partition Array by Odd and Even
- 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
- LintCode on Array by Odd and Even
- Partition Array by Odd and Even 解题报告
- [剑指offer][第三章][14]Partition Array by Odd and Even
- 373.Partition Array by Odd and Even-奇偶分割数组(容易题)
- lintcode:Partition Array
- lintcode:Partition Array
- LintCode Partition Array
- Rearrange int array to put odd number to left and even to right
- Odd-even sort: Sorting by Exchanging
- CF621A - Wet Shark and Odd and Even
- A. Wet Shark and Odd and Even
- Wet Shark and Odd and Even
- [leetcode Q9] Container With Most Water
- C#Winform不重复的显示子窗体
- Memory Management(1)
- <Java编程思想> 阅读 (1) 2.1 .2.2
- javaScript学习笔记(二)
- [Lintcode]Partition Array by Odd and Even
- 18. 4Sum
- 城市建设
- 监听者—观察者模式
- oc之NSlog用法【2】
- iOS-多线程编程学习之NSOperation(四)
- Failed to convert value of type...no matching editors or conversion strategy found
- Javascript自学-7
- linux 常见测试题