剑指offer No.14,这题用双指针就可以了,代码如下
package com.zjy.sword2offer;
public class ReorderArrByOddEven {
public static void reorderArr(int[] a){
if(a==null||a.length<=0)
return;
int Len = a.length;
int pfront = 0;
int pback = Len-1;
while(true)
{
while(pfront<Len-1)
{
if((a[pfront]&1)==1)
pfront++;
else
break;
}
while(pback>0)
{
if((a[pback]&1)==0)
pback--;
else
break;
}
if(pfront>=pback)
break;
else
{
int tmp = a[pfront];
a[pfront] = a[pback];
a[pback] = tmp;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {2};
reorderArr(null);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}