FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_01/id_26/Leetcode_905_26.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_26
/
Leetcode_905_26.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (38 loc) · 1.27 KB
Breadcrumbs
algorithm
/
Week_01
/
id_26
/
Leetcode_905_26.java
Copy path
File metadata and controls
44 lines (38 loc) · 1.27 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
// Source : https://leetcode.com/problems/sort-array-by-parity/
// Id : 905
// Author : Fanlu Hai
// Date : 2018-04-15
import
java
.
util
.
Arrays
;
class
SortArrayByParity
{
public
int
[]
sortArrayByParity
(
int
[]
A
) {
int
i
=
0
;
int
j
=
A
.
length
-
1
;
while
(
j
>
i
) {
// System.out.println(Arrays.toString(A));
if
(
A
[
i
] %
2
==
1
&&
A
[
j
] %
2
==
0
) {
swapNumInArray
(
i
,
j
,
A
);
}
if
(
A
[
i
] %
2
==
0
) {
i
++;
//in order to make sure a check is performed
continue
;
}
if
(
A
[
j
] %
2
==
1
) {
j
--;
//in order to make sure a check is performed
continue
;
}
}
return
A
;
}
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
) {
SortArrayByParity
sortArrayByParity
=
new
SortArrayByParity
();
int
[]
a
= {
1
,
1
,
1
,
1
,
2
,
2
,
2
,
3
,
3
,
33
,
4
,
4
,
5
,
5
,
5
,
5
,
6
,
6
,
7
,
7
,
7
,
8
};
System
.
out
.
println
(
Arrays
.
toString
(
sortArrayByParity
.
sortArrayByParity
(
a
)));
}
}
Back
|
FazBrowse Home
|
New Git URL