Partition Array by Odd and Even
2017-10-17 21:52
239 查看
Partition an integers array into odd number first and even number second.
Have you met this question in a real interview? Yes
Example
Given [1, 2, 3, 4], return [1, 3, 2, 4]
java
python
Have you met this question in a real interview? Yes
Example
Given [1, 2, 3, 4], return [1, 3, 2, 4]
java
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 == 0 || nums.length == 1) { return; } int left = 0; int right = nums.length - 1; while (left <= right) { while (left <= right && nums[left] % 2 != 0) { left++; } while (left <= right && nums[right] % 2 == 0) { right--; } if (left <= right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } } } }
python
class Solution: """ @param: nums: an array of integers @return: nothing """ def partitionArray(self, nums): # write your code here if nums == None or len(nums) == 0 or len(nums) == 1: return left, right = 0, len(nums) - 1 while left <= right: while left <= right and nums[left] % 2 != 0: left += 1 while left <= right and nums[right] % 2 == 0: right -= 1 if left <= right: nums[left], nums[right] = nums[right], nums[left]
相关文章推荐
- #373 Partition Array by Odd and Even
- Partition Array by Odd and Even
- lintcode-easy-Partition Array by Odd and Even
- Lintcode373 Partition Array by Odd and Even solution 题解
- [剑指offer][第三章][14]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 解题报告
- [Lintcode]Partition Array by Odd and Even
- LintCode "Partition Array by Odd and Even"
- LintCode on Array by Odd and Even
- separate odd and even numbers in an array in O(n) time complexity and O(1) space complexity
- Rearrange int array to put odd number to left and even to right
- Odd-even sort: Sorting by Exchanging
- Swap all odd and even bits
- UVa 12683 Odd and Even Zeroes(数论+数字DP)
- 【CodeForces】#621A - Wet Shark and Odd and Even(水)
- K - Wet Shark and Odd and Even(第三场)
- 【codeforces】-#621-AWet Shark and Odd and Even(n个数最大偶数和)
- A. Wet Shark and Odd and Even