美文网首页程序员
[Leetcode]905. Sort Array By Par

[Leetcode]905. Sort Array By Par

作者: 麻薯团子子 | 来源:发表于2018-10-04 17:04 被阅读0次

一、问题链接:
https://leetcode.com/problems/sort-array-by-parity/description/
二、思路:
看完描述之后,对odd和even这两个词汇反应了几秒,然后是偶数前置,奇数后置的一个操作。
三、编码:
public int[] range(int[] a) {
if (a.length < 2) {
return a;
}
int[] b = new int[a.length];
int left = 0;
int right = a.length -1;
for (int i = 0; i < a.length && left <= right; i++) {
if (a[i] % 2 == 0) {
b[left] = a[i];
left++;
}else {
b[right] = a[i];
right --;
}
}
return b;
}

相关文章

网友评论

    本文标题:[Leetcode]905. Sort Array By Par

    本文链接:https://www.haomeiwen.com/subject/esixaftx.html