FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_109/LeetCode_905_109.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
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_109
/
LeetCode_905_109.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (46 loc) · 1.04 KB
Breadcrumbs
algorithm
/
Week_01
/
id_109
/
LeetCode_905_109.java
Copy path
File metadata and controls
51 lines (46 loc) · 1.04 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
49
50
51
/**
* @Auther: quanhc
* @Date: 2019-04-20 21:17
* @Description:
*/
public
class
LeetCode905
{
public
int
[]
sortArrayByParity
(
int
[]
A
) {
int
[]
result
=
new
int
[
A
.
length
];
int
j
=
A
.
length
-
1
;
int
k
=
0
;
for
(
int
i
=
0
;
i
<
A
.
length
;
i
++){
if
(
A
[
i
]%
2
==
0
){
result
[
k
] =
A
[
i
];
k
++;
}
else
{
result
[
j
] =
A
[
i
];
j
--;
}
}
return
result
;
}
/**
* 双指针.但不知为啥从空间和时间复杂度 都没有额外的数组快
* @param A
* @return
*/
public
int
[]
sortArrayByParity
(
int
[]
A
) {
int
i
=
0
;
int
k
=
A
.
length
-
1
;
while
(
i
<
k
){
if
(
A
[
i
]%
2
!=
0
&&
A
[
k
]%
2
==
0
){
int
tmp
=
A
[
i
];
A
[
i
] =
A
[
k
];
A
[
k
] =
tmp
;
continue
;
}
if
(
A
[
i
]%
2
==
0
){
i
++;
}
if
(
A
[
k
]%
2
!=
0
){
k
--;
}
}
return
A
;
}
}
Back
|
FazBrowse Home
|
New Git URL