Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53379c0

Browse files
committed
添加 0454.四数相加II.md Scala版本
1 parent abe0023 commit 53379c0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎problems/0454.四数相加II.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,5 +318,41 @@ impl Solution {
318318
}
319319
```
320320

321+
Scala:
322+
```scala
323+
object Solution {
324+
// 导包
325+
import scala.collection.mutable
326+
def fourSumCount(nums1: Array[Int], nums2: Array[Int], nums3: Array[Int], nums4: Array[Int]): Int = {
327+
// 定义一个HashMap,key存储值,value存储该值出现的次数
328+
val map = new mutable.HashMap[Int, Int]()
329+
// 遍历前两个数组,把他们所有可能的情况都记录到map
330+
for (i <- nums1.indices) {
331+
for (j <- nums2.indices) {
332+
val tmp = nums1(i) + nums2(j)
333+
// 如果包含该值,则对他的key加1,不包含则添加进去
334+
if (map.contains(tmp)) {
335+
map.put(tmp, map.get(tmp).get + 1)
336+
} else {
337+
map.put(tmp, 1)
338+
}
339+
}
340+
}
341+
var res = 0 // 结果变量
342+
// 遍历后两个数组
343+
for (i <- nums3.indices) {
344+
for (j <- nums4.indices) {
345+
val tmp = -(nums3(i) + nums4(j))
346+
// 如果map中存在该值,结果就+=value
347+
if (map.contains(tmp)) {
348+
res += map.get(tmp).get
349+
}
350+
}
351+
}
352+
// 返回最终结果,可以省略关键字return
353+
res
354+
}
355+
}
356+
```
321357
-----------------------
322358
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /