FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LeetCode/ProductOfArrayExceptSelf.java at master · Orio77/LeetCode · GitHub
Orio77
/
LeetCode
Public
Notifications
You must be signed in to change notification settings
Fork
0
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
LeetCode
/
ProductOfArrayExceptSelf.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
34 lines (28 loc) · 978 Bytes
Breadcrumbs
LeetCode
/
ProductOfArrayExceptSelf.java
Copy path
File metadata and controls
34 lines (28 loc) · 978 Bytes
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
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
public
class
ProductOfArrayExceptSelf
{
public
int
[]
productExceptSelf
(
int
[]
nums
) {
if
(
nums
==
null
||
nums
.
length
==
0
) {
return
new
int
[
0
];
}
Map
<
Integer
,
Integer
>
backward
=
new
HashMap
<>();
Map
<
Integer
,
Integer
>
forward
=
new
HashMap
<>();
backward
.
put
(
0
,
nums
[
0
]);
forward
.
put
(
nums
.
length
-
1
,
nums
[
nums
.
length
-
1
]);
int
i
=
1
;
int
j
=
nums
.
length
-
2
;
while
(
i
<
nums
.
length
) {
backward
.
put
(
i
,
nums
[
i
]*
backward
.
get
(
i
-
1
));
forward
.
put
(
j
,
nums
[
j
]*
forward
.
get
(
j
+
1
));
i
++;
j
--;
}
int
[]
result
=
new
int
[
nums
.
length
];
result
[
0
] =
forward
.
get
(
1
);
result
[
nums
.
length
-
1
] =
backward
.
get
(
nums
.
length
-
2
);
for
(
int
k
=
1
;
k
<
result
.
length
-
1
;
k
++) {
result
[
k
] =
backward
.
get
(
k
-
1
) *
forward
.
get
(
k
+
1
);
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL