FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_26/Leetcode_922_26.java at master · algorithm001/algorithm · GitHub
algorithm001
/
algorithm
Public
Notifications
You must be signed in to change notification settings
Fork
148
Star
118
Code
Issues
548
Pull requests
46
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_922_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (40 loc) · 1.37 KB
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_922_26.java
Copy path
File metadata and controls
50 lines (40 loc) · 1.37 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
// Source : https://leetcode.com/problems/sort-array-by-parity-ii/
// Id : 922
// Author : Fanlu Hai
// Date : 2018-04-15
import
java
.
util
.
Arrays
;
class
SortArrayByParityII
{
public
int
[]
sortArrayByParityII
(
int
[]
A
) {
int
i
=
0
;
int
j
=
1
;
while
(
true
) {
// System.out.println(A.length + " " + i + " " + j + " " + Arrays.toString(A));
if
(
i
>=
A
.
length
||
j
>=
A
.
length
) {
return
A
;
}
if
(
A
[
i
] %
2
==
1
&&
A
[
j
] %
2
==
0
) {
swapNumInArray
(
i
,
j
,
A
);
}
if
(
A
[
i
] %
2
==
0
) {
i
+=
2
;
//in order to make sure a check is performed
continue
;
}
if
(
A
[
j
] %
2
==
1
) {
j
+=
2
;
//in order to make sure a check is performed
continue
;
}
}
}
public
void
swapNumInArray
(
int
firstIndex
,
int
secondIndex
,
int
[]
array
) {
int
tmp
=
array
[
firstIndex
];
array
[
firstIndex
] =
array
[
secondIndex
];
array
[
secondIndex
] =
tmp
;
}
public
static
void
main
(
String
[]
args
) {
SortArrayByParityII
sortArrayByParityII
=
new
SortArrayByParityII
();
int
[]
a
= {
1
,
1
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
0
,
2
,
2
};
System
.
out
.
println
(
Arrays
.
toString
(
sortArrayByParityII
.
sortArrayByParityII
(
a
)));
}
}
Back
|
FazBrowse Home
|
New Git URL