FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_118/leetcode_905_118.java at master · kdbreboot/algorithm · GitHub
kdbreboot
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_905_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (43 loc) · 1.19 KB
Breadcrumbs
algorithm
/
Week_01
/
id_118
/
leetcode_905_118.java
Copy path
File metadata and controls
48 lines (43 loc) · 1.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* https://leetcode-cn.com/problems/sort-array-by-parity/submissions/
*/
class
Solution
{
public
int
[]
sortArrayByParity
(
int
[]
A
) {
// 1. 边界处理,长度 <=1 什么都不做
if
(
A
.
length
<=
1
){
return
A
;
}
// 2. 定义前后索引
int
left
=
0
;
int
right
=
A
.
length
-
1
;
// 3. 移动元素
while
(
left
<
right
){
// 若,左侧奇数,右侧偶数,则交换元素。
if
(
isOdd
(
A
[
left
]) &&
isEven
(
A
[
right
])){
int
tmp
=
A
[
left
];
A
[
left
] =
A
[
right
];
A
[
right
] =
tmp
;
left
++;
right
--;
}
else
{
// 左侧是偶数,则右移一位
if
(
isEven
(
A
[
left
])){
left
++;
}
// 右侧是奇数,则左移一位
if
(
isOdd
(
A
[
right
])){
right
--;
}
}
}
return
A
;
}
// 是否为偶数
boolean
isEven
(
int
x
){
return
x
%
2
==
0
;
}
// 是否为奇数
boolean
isOdd
(
int
x
){
return
x
%
2
!=
0
;
}
}
Back
|
FazBrowse Home
|
New Git URL