@@ -318,5 +318,41 @@ impl Solution {
318
318
}
319
319
```
320
320
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
+ ```
321
357
-----------------------
322
358
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img ></div >
0 commit comments