FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_139/LeetCode_922_139.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_139
/
LeetCode_922_139.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (54 loc) · 1.8 KB
Breadcrumbs
algorithm
/
Week_01
/
id_139
/
LeetCode_922_139.java
Copy path
File metadata and controls
64 lines (54 loc) · 1.8 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
52
53
54
55
56
57
58
59
60
61
62
63
64
1
,
建立新数组
//执行用时 : 6 ms, 在Sort Array By Parity II的Java提交中击败了83.69% 的用户
//内存消耗 : 48.9 MB, 在Sort Array By Parity II的Java提交中击败了67.91% 的用户
class
Solution
{
public
int
[]
sortArrayByParityII
(
int
[]
A
) {
if
(
A
==
null
||
A
.
length
==
1
)
return
A
;
int
k
=
A
.
length
-
1
;
int
[]
B
=
new
int
[
A
.
length
];
int
i
=
0
;
int
j
=
0
;
int
x
=
1
;
while
(
i
<=
k
){
if
(
A
[
i
]%
2
==
0
){
B
[
j
] =
A
[
i
];
j
=
j
+
2
;
}
else
{
B
[
x
] =
A
[
i
];
x
=
x
+
2
;
}
i
++;
}
return
B
;
}
}
2
,
//执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户
//内存消耗 : 49.7 MB, 在Sort Array By Parity II的Java提交中击败了51.80% 的用户
//执行用时 : 5 ms, 在Sort Array By Parity II的Java提交中击败了93.39% 的用户
//内存消耗 : 41.5 MB, 在Sort Array By Parity II的Java提交中击败了92.67% 的用户
//执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户
//内存消耗 : 52.3 MB, 在Sort Array By Parity II的Java提交中击败了9.49% 的用户
class
Solution
{
public
int
[]
sortArrayByParityII
(
int
[]
A
) {
if
(
A
==
null
||
A
.
length
==
1
)
return
A
;
int
k
=
A
.
length
-
1
;
int
i
=
0
;
int
j
=
1
;
while
(
i
<=
k
&&
j
<=
k
){
if
(
A
[
i
]%
2
!=
0
&&
A
[
j
]%
2
==
0
){
int
temp
=
A
[
i
];
A
[
i
] =
A
[
j
];
A
[
j
]=
temp
;
i
=
i
+
2
;
j
=
j
+
2
;
}
else
{
if
(
A
[
i
]%
2
==
0
)
i
=
i
+
2
;
if
(
A
[
j
]%
2
!=
0
)
j
=
j
+
2
;
}
}
return
A
;
}
}
Back
|
FazBrowse Home
|
New Git URL