Partition Array by Odd and Even
来源:互联网 发布:网页js代码 编辑:程序博客网 时间:2024/06/08 17:39
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[] arr) { /* Initialize left and right indexes */ int left = 0, right = arr.length - 1; while (left < right) { /* Increment left index while we see 0 at left */ while (arr[left]%2 == 0 && left < right) left++; /* Decrement right index while we see 1 at right */ while (arr[right]%2 == 1 && left < right) right--; if (left < right) { /* Swap arr[left] and arr[right]*/ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } }}
0 0
- 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
- 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)
- iOS 第三方框架SDWebImage的简单使用
- paintEvent()重绘事件
- 常见java异常
- 【LEETCODE】191-Number of 1 Bits
- RecyclerView 设置 itemclickListener 和 longclicklintener
- Partition Array by Odd and Even
- recvfrom接收udp包代码,注释中写明了几点注意事项
- Keil uVision(MDK)编译错误积累
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- 函数式思维: 函数设计模式,第 1 部分
- IBM AIX 系统Lpar学习以及资源调整
- 线性表的顺序查找
- Python使用getopt处理命令行操作
- 如何用MYSQL建立新表,并复制旧表的结构