FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaArithmetic/src/LeetCode/Leetcode454.java at master · wpq2014/JavaArithmetic · GitHub
wpq2014
JavaArithmetic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaArithmetic
/
src
/
LeetCode
/
Leetcode454.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (65 loc) · 2.54 KB
Breadcrumbs
JavaArithmetic
/
src
/
LeetCode
/
Leetcode454.java
Copy path
File metadata and controls
82 lines (65 loc) · 2.54 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package
LeetCode
;
import
java
.
util
.
HashMap
;
public
class
Leetcode454
{
// https://leetcode.com/problems/4sum-ii/description/
// 时间复杂度: O(n^2)
// 空间复杂度: O(n^2)
public
int
fourSumCount
(
int
[]
A
,
int
[]
B
,
int
[]
C
,
int
[]
D
) {
if
(
A
==
null
||
B
==
null
||
C
==
null
||
D
==
null
)
throw
new
IllegalArgumentException
(
"Illegal argument"
);
// 计算C和D的sum,放入Map,如果sum重复的话,value加1
HashMap
<
Integer
,
Integer
>
map
=
new
HashMap
<
Integer
,
Integer
>();
for
(
int
i
=
0
;
i
<
C
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
D
.
length
;
j
++){
int
sum
=
C
[
i
] +
D
[
j
];
if
(
map
.
containsKey
(
sum
))
map
.
put
(
sum
,
map
.
get
(
sum
) +
1
);
else
map
.
put
(
sum
,
1
);
}
//1.A+B+C+D=0---->A+B+Z=0--->A+B=-Z-->Z=-(A+B);
int
res
=
0
;
for
(
int
i
=
0
;
i
<
A
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
B
.
length
;
j
++)
if
(
map
.
containsKey
(-
A
[
i
]-
B
[
j
]))
res
+=
map
.
get
(-
A
[
i
]-
B
[
j
]);
return
res
;
}
// 时间复杂度: O(n^2)
// 空间复杂度: O(n^2)
public
int
fourSumCount2
(
int
[]
A
,
int
[]
B
,
int
[]
C
,
int
[]
D
) {
if
(
A
==
null
||
B
==
null
||
C
==
null
||
D
==
null
)
throw
new
IllegalArgumentException
(
"Illegal argument"
);
HashMap
<
Integer
,
Integer
>
mapAB
=
new
HashMap
<
Integer
,
Integer
>();
for
(
int
i
=
0
;
i
<
A
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
B
.
length
;
j
++){
int
sum
=
A
[
i
] +
B
[
j
];
if
(
mapAB
.
containsKey
(
sum
))
mapAB
.
put
(
sum
,
mapAB
.
get
(
sum
) +
1
);
else
mapAB
.
put
(
sum
,
1
);
}
HashMap
<
Integer
,
Integer
>
mapCD
=
new
HashMap
<
Integer
,
Integer
>();
for
(
int
i
=
0
;
i
<
C
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
D
.
length
;
j
++){
int
sum
=
C
[
i
] +
D
[
j
];
if
(
mapCD
.
containsKey
(
sum
))
mapCD
.
put
(
sum
,
mapCD
.
get
(
sum
) +
1
);
else
mapCD
.
put
(
sum
,
1
);
}
int
res
=
0
;
for
(
Integer
sumab
:
mapAB
.
keySet
()){
if
(
mapCD
.
containsKey
(-
sumab
))
res
+=
mapAB
.
get
(
sumab
) *
mapCD
.
get
(-
sumab
);
}
return
res
;
}
public
static
void
main
(
String
[]
args
) {
int
[]
a
= {
1
,
2
};
int
[]
b
= {-
2
, -
1
};
int
[]
c
= {-
1
,
2
};
int
[]
d
= {
0
,
2
};
System
.
out
.
println
((
new
Leetcode454
()).
fourSumCount
(
a
,
b
,
c
,
d
));
}
}
Back
|
FazBrowse Home
|
New Git URL