LintCode on Array by Odd and Even
来源:互联网 发布:mac book air视频剪辑 编辑:程序博客网 时间:2024/05/18 17:40
description:
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]
非常简单,直接使用two pointers 的算法就能够非常好的处理。
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) { if (left >= right) { return; } while (left <= right && nums[left] % 2 == 1) { 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--; } } }}
0 0
- LintCode on Array by Odd and Even
- [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
- Partition Array by Odd and Even 解题报告
- [剑指offer][第三章][14]Partition Array by Odd and Even
- 373.Partition Array by Odd and Even-奇偶分割数组(容易题)
- 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
- Wet Shark and Odd and Even
- Swap all odd and even bits
- A even and odd(13.7.6)
- c#单元测试从零到会用
- opencv之cvContourArea计算整个或部分轮廓的面积
- 深度学习-目标检测综述
- 在 Linux 下搭建 Git 服务器
- SVN简介
- LintCode on Array by Odd and Even
- 前端培训班出身——吐槽下16年的艰辛
- Linux Day4
- 生成式对抗网络(Generative Adversarial Nets)
- struts2.5初步使用教程
- Android EditText获取焦点并弹出软键盘
- CentOS下使用yum安装Git具体步骤
- 文档手册
- 一点公益系统模式源码