题目链接:https://leetcode-cn.com/problems/sort-array-by-parity/
给定一个非负整数数组 A
,返回一个由 A
的所有偶数元素组成的数组,后面跟 A
的所有奇数元素。
你可以返回满足此条件的任何数组作为答案。
示例:
输入:[3,1,2,4] 输出:[2,4,3,1] 输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
提示:
1 <= A.length <= 5000
0 <= A[i] <= 5000
很简单的一道题
object Solution {
import scala.collection.mutable.ArrayBuffer
def sortArrayByParity(A: Array[Int]): Array[Int] = {
var jtemp = new ArrayBuffer[Int]
var otemp = new ArrayBuffer[Int]
for(i <- 0 to A.length-1){
if(A(i)%2==0)
otemp += A(i)
else
jtemp += A(i)
}
var temp = otemp.toList.sorted ++ jtemp.toList.sorted
temp.toArray
}
}