-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray922.java
More file actions
63 lines (61 loc) · 1.55 KB
/
Array922.java
File metadata and controls
63 lines (61 loc) · 1.55 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
59
60
61
62
63
package array;
/**
* @ProjectName: leetcode
* @Package: array
* @ClassName: Array922
* @Author: markey
* @Description:
* 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数。
*
* 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数;当 A[i] 为偶数时, i 也是偶数。
*
* 你可以返回任何满足上述条件的数组作为答案。
*
*
*
* 示例:
*
* 输入:[4,2,5,7]
* 输出:[4,5,2,7]
* 解释:[4,7,2,5],[2,5,4,7],[2,7,4,5] 也会被接受。
*
*
* 提示:
*
* 2 <= A.length <= 20000
* A.length % 2 == 0
* 0 <= A[i] <= 1000
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/sort-array-by-parity-ii
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @Date: 2019/10/17 23:06
* @Version: 1.0
*/
public class Array922 {
/**
* 执行用时 :3 ms, 在所有 java 提交中击败了97.24%的用户
* 内存消耗 :40.4 MB, 在所有 java 提交中击败了95.19%的用户
* @param A
* @return
*/
public int[] sortArrayByParityII(int[] A) {
int i = 0, j = A.length -1;
while (i < A.length) {
if (A[i]%2 == 0) {
i += 2;
continue;
}
if (A[j]%2 != 0) {
j -= 2;
continue;
}
int temp = A[i];
A[i] = A[j];
A[j] = temp;
i += 2;
j -= 2;
}
return A;
}
}