FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_04/id_30/LeetCode_169_30.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_04
/
id_30
/
LeetCode_169_30.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
64 lines (58 loc) · 1.89 KB
Breadcrumbs
algorithm
/
Week_04
/
id_30
/
LeetCode_169_30.java
Copy path
File metadata and controls
64 lines (58 loc) · 1.89 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
package
com
.
shufeng
.
algorithm4
.
demo
;
import
java
.
util
.
Random
;
/**
* @author gsf
* @date 2019-05-11 22:41
*/
public
class
LeetCode_169_30
{
public
static
void
main
(
String
[]
args
) {
int
[]
arr
= {
47
,
47
,
72
,
47
,
72
,
47
,
79
,
47
,
12
,
92
,
13
,
47
,
47
,
83
,
33
,
15
,
18
,
47
,
47
,
47
,
47
,
64
,
47
,
65
,
47
,
47
,
47
,
47
,
70
,
47
,
47
,
55
,
47
,
15
,
60
,
47
,
47
,
47
,
47
,
47
,
46
,
30
,
58
,
59
,
47
,
47
,
47
,
47
,
47
,
90
,
64
,
37
,
20
,
47
,
100
,
84
,
47
,
47
,
47
,
47
,
47
,
89
,
47
,
36
,
47
,
60
,
47
,
18
,
47
,
34
,
47
,
47
,
47
,
47
,
47
,
22
,
47
,
54
,
30
,
11
,
47
,
47
,
86
,
47
,
55
,
40
,
49
,
34
,
19
,
67
,
16
,
47
,
36
,
47
,
41
,
19
,
80
,
47
,
47
,
27
};
int
i
=
majorityElement
(
arr
);
System
.
out
.
println
(
i
);
}
public
static
int
majorityElement
(
int
[]
nums
) {
quickSort1
(
nums
,
0
,
nums
.
length
-
1
);
return
nums
[
nums
.
length
/
2
];
}
/**
* leetcode 执行和提交 结果不一致,提交代码不过。
* 三路快排
* 数组根据基数分成三份<、=、>
*/
private
static
void
quickSort1
(
int
[]
nums
,
int
l
,
int
r
) {
if
(
l
>=
r
) {
return
;
}
// 随机数基数
int
s
=
new
Random
().
nextInt
(
nums
.
length
);
swap1
(
nums
,
l
,
s
);
int
v
=
nums
[
l
];
// 左分点,分割小于和等于,i左边小v,i~k 等于v
int
i
=
l
;
// 循环点
int
k
=
l
+
1
;
// 右分点,右边大于v的数
int
j
=
r
+
1
;
while
(
k
<
j
) {
if
(
nums
[
k
] <
v
) {
// 循环数和最左边等于V的数交换
swap1
(
nums
,
k
,
i
+
1
);
i
++;
k
++;
}
else
if
(
nums
[
k
] >
v
) {
swap1
(
nums
,
k
,
j
-
1
);
j
--;
}
else
{
k
++;
}
}
swap1
(
nums
,
l
,
i
);
quickSort1
(
nums
,
l
,
i
-
1
);
quickSort1
(
nums
,
j
,
r
);
}
private
static
void
swap1
(
int
[]
arr
,
int
i
,
int
j
) {
int
tem
=
arr
[
i
];
arr
[
i
] =
arr
[
j
];
arr
[
j
] =
tem
;
}
}
Back
|
FazBrowse Home
|
New Git URL