FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OSSDP-Lab2-acceptPR/Solution4.java at main · MortusCc/OSSDP-Lab2-acceptPR · GitHub
MortusCc
/
OSSDP-Lab2-acceptPR
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution4.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
63 lines (59 loc) · 1.61 KB
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution4.java
Copy path
File metadata and controls
63 lines (59 loc) · 1.61 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
import
java
.
util
.
Arrays
;
/**
* @description:
*
* 给定一个无序的数组 nums,返回 数组在排序之后,相邻元素之间最大的差值 。如果数组元素个数小于 2,则返回 0 。
*
* 您必须编写一个在「线性时间」内运行并使用「线性额外空间」的算法。
*
*
*
* 示例 1:
*
* 输入: nums = [3,6,9,1]
* 输出: 3
* 解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
* 示例 2:
*
* 输入: nums = [10]
* 输出: 0
* 解释: 数组元素个数小于 2,因此返回 0。
*
*
* 提示:
*
* 1 <= nums.length <= 105
* 0 <= nums[i] <= 109
*
*/
class
Solution4
{
public
int
maximumGap
(
int
[]
nums
) {
int
n
=
nums
.
length
-
1
;
if
(
n
<
2
) {
return
0
;
}
long
exp
=
1
;
int
[]
buf
=
new
int
[
n
];
int
maxVal
=
Arrays
.
stream
(
nums
).
max
().
getAsInt
();
while
(
maxVal
>
exp
) {
int
[]
cnt
=
new
int
[
10
];
for
(
int
i
=
0
;
i
<
n
;
i
++) {
int
digit
= (
nums
[
i
] / (
int
)
exp
) %
10
;
cnt
[
digit
]++;
}
for
(
int
i
=
1
;
i
<
10
;
i
++){
cnt
[
i
] +=
cnt
[
i
-
1
];
for
(
int
i
=
n
-
1
;
i
>=
0
;
i
--) {
int
digit
= (
nums
[
i
] / (
int
)
exp
) %
10
;
buf
[
cnt
[
digit
] -
1
] =
nums
[
i
];
cnt
[
digit
]--;
}
System
.
arraycopy
(
buf
,
0
,
nums
,
0
,
n
);
exp
+=
10
;
}
int
ret
=
0
;
for
(
int
i
=
1
;
i
<
n
;
i
++) {
ret
=
Math
.
max
(
ret
,
nums
[
i
] -
nums
[
i
-
1
]);
}
return
ret
;
}
}
Back
|
FazBrowse Home
|
New Git URL