Skip to content

Commit 71a9111

Browse files
Merge pull request youngyangyang04#1326 from wzqwtt/patch08
添加(0001.两数之和、0454.四数相加II、0383.赎金信)Scala版本
2 parents f03f8d2 + 8c2737d commit 71a9111

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

problems/0001.两数之和.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,30 @@ class Solution {
274274
}
275275
}
276276
```
277+
278+
Scala:
279+
```scala
280+
object Solution {
281+
// 导入包
282+
import scala.collection.mutable
283+
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
284+
// key存储值,value存储下标
285+
val map = new mutable.HashMap[Int, Int]()
286+
for (i <- nums.indices) {
287+
val tmp = target - nums(i) // 计算差值
288+
// 如果这个差值存在于map,则说明找到了结果
289+
if (map.contains(tmp)) {
290+
return Array(map.get(tmp).get, i)
291+
}
292+
// 如果不包含把当前值与其下标放到map
293+
map.put(nums(i), i)
294+
}
295+
// 如果没有找到直接返回一个空的数组,return关键字可以省略
296+
new Array[Int](2)
297+
}
298+
}
299+
```
300+
277301
C#:
278302
```csharp
279303
public class Solution {
@@ -293,5 +317,6 @@ public class Solution {
293317
}
294318
```
295319

320+
296321
-----------------------
297322
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0383.赎金信.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,70 @@ impl Solution {
363363
}
364364
```
365365

366+
Scala:
367+
368+
版本一: 使用数组作为哈希表
369+
```scala
370+
object Solution {
371+
def canConstruct(ransomNote: String, magazine: String): Boolean = {
372+
// 如果magazine的长度小于ransomNote的长度,必然是false
373+
if (magazine.length < ransomNote.length) {
374+
return false
375+
}
376+
// 定义一个数组,存储magazine字符出现的次数
377+
val map: Array[Int] = new Array[Int](26)
378+
// 遍历magazine字符串,对应的字符+=1
379+
for (i <- magazine.indices) {
380+
map(magazine(i) - 'a') += 1
381+
}
382+
// 遍历ransomNote
383+
for (i <- ransomNote.indices) {
384+
if (map(ransomNote(i) - 'a') > 0)
385+
map(ransomNote(i) - 'a') -= 1
386+
else return false
387+
}
388+
// 如果上面没有返回false,直接返回true,关键字return可以省略
389+
true
390+
}
391+
}
392+
```
393+
394+
```scala
395+
object Solution {
396+
import scala.collection.mutable
397+
def canConstruct(ransomNote: String, magazine: String): Boolean = {
398+
// 如果magazine的长度小于ransomNote的长度,必然是false
399+
if (magazine.length < ransomNote.length) {
400+
return false
401+
}
402+
// 定义map,key是字符,value是字符出现的次数
403+
val map = new mutable.HashMap[Char, Int]()
404+
// 遍历magazine,把所有的字符都记录到map里面
405+
for (i <- magazine.indices) {
406+
val tmpChar = magazine(i)
407+
// 如果map包含该字符,那么对应的value++,否则添加该字符
408+
if (map.contains(tmpChar)) {
409+
map.put(tmpChar, map.get(tmpChar).get + 1)
410+
} else {
411+
map.put(tmpChar, 1)
412+
}
413+
}
414+
// 遍历ransomNote
415+
for (i <- ransomNote.indices) {
416+
val tmpChar = ransomNote(i)
417+
// 如果map包含并且该字符的value大于0,则匹配成功,map对应的--,否则直接返回false
418+
if (map.contains(tmpChar) && map.get(tmpChar).get > 0) {
419+
map.put(tmpChar, map.get(tmpChar).get - 1)
420+
} else {
421+
return false
422+
}
423+
}
424+
// 如果上面没有返回false,直接返回true,关键字return可以省略
425+
true
426+
}
427+
}
428+
429+
366430
C#
367431
```csharp
368432
public bool CanConstruct(string ransomNote, string magazine) {
@@ -379,6 +443,7 @@ public bool CanConstruct(string ransomNote, string magazine) {
379443
}
380444
return true;
381445
}
446+
382447
```
383448
-----------------------
384449
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0454.四数相加II.md

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

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

0 commit comments

Comments
 (0)