-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray905.java
More file actions
58 lines (57 loc) · 1.46 KB
/
Array905.java
File metadata and controls
58 lines (57 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array905
* @Author: markey
* @Description:
* 给定一个非负整数数组 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
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/sort-array-by-parity
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/10/17 23:05
* @Version: 1.0
*/
public class Array905 {
/**
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Sort Array By Parity.
* Memory Usage: 40.3 MB, less than 92.36% of Java online submissions for Sort Array By Parity.
* @param A
* @return
*/
public int[] sortArrayByParity(int[] A) {
for (int i = 0, j = A.length - 1; i < j;) {
if (A[i] %2 == 0) {
i++;
continue;
}
if (A[j] %2 == 1) {
j--;
continue;
}
int temp = A[i];
A[i] = A[j];
A[j] = temp;
i++;
j--;
}
return A;
}
}