Merge pull request #1326 from wzqwtt/patch08

添加(0001.两数之和、0454.四数相加II、0383.赎金信)Scala版本
This commit is contained in:
程序员Carl
2022-06-04 09:08:19 +08:00
committed by GitHub
3 changed files with 127 additions and 0 deletions

View File

@ -274,6 +274,30 @@ class Solution {
}
}
```
Scala:
```scala
object Solution {
// 导入包
import scala.collection.mutable
def twoSum(nums: Array[Int], target: Int): Array[Int] = {
// key存储值value存储下标
val map = new mutable.HashMap[Int, Int]()
for (i <- nums.indices) {
val tmp = target - nums(i) // 计算差值
// 如果这个差值存在于map则说明找到了结果
if (map.contains(tmp)) {
return Array(map.get(tmp).get, i)
}
// 如果不包含把当前值与其下标放到map
map.put(nums(i), i)
}
// 如果没有找到直接返回一个空的数组return关键字可以省略
new Array[Int](2)
}
}
```
C#:
```csharp
public class Solution {
@ -293,5 +317,6 @@ public class Solution {
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -363,6 +363,70 @@ impl Solution {
}
```
Scala:
版本一: 使用数组作为哈希表
```scala
object Solution {
def canConstruct(ransomNote: String, magazine: String): Boolean = {
// 如果magazine的长度小于ransomNote的长度必然是false
if (magazine.length < ransomNote.length) {
return false
}
// 定义一个数组存储magazine字符出现的次数
val map: Array[Int] = new Array[Int](26)
// 遍历magazine字符串对应的字符+=1
for (i <- magazine.indices) {
map(magazine(i) - 'a') += 1
}
// 遍历ransomNote
for (i <- ransomNote.indices) {
if (map(ransomNote(i) - 'a') > 0)
map(ransomNote(i) - 'a') -= 1
else return false
}
// 如果上面没有返回false直接返回true关键字return可以省略
true
}
}
```
```scala
object Solution {
import scala.collection.mutable
def canConstruct(ransomNote: String, magazine: String): Boolean = {
// 如果magazine的长度小于ransomNote的长度必然是false
if (magazine.length < ransomNote.length) {
return false
}
// 定义mapkey是字符value是字符出现的次数
val map = new mutable.HashMap[Char, Int]()
// 遍历magazine把所有的字符都记录到map里面
for (i <- magazine.indices) {
val tmpChar = magazine(i)
// 如果map包含该字符那么对应的value++,否则添加该字符
if (map.contains(tmpChar)) {
map.put(tmpChar, map.get(tmpChar).get + 1)
} else {
map.put(tmpChar, 1)
}
}
// 遍历ransomNote
for (i <- ransomNote.indices) {
val tmpChar = ransomNote(i)
// 如果map包含并且该字符的value大于0则匹配成功map对应的--否则直接返回false
if (map.contains(tmpChar) && map.get(tmpChar).get > 0) {
map.put(tmpChar, map.get(tmpChar).get - 1)
} else {
return false
}
}
// 如果上面没有返回false直接返回true关键字return可以省略
true
}
}
C#
```csharp
public bool CanConstruct(string ransomNote, string magazine) {
@ -379,6 +443,7 @@ public bool CanConstruct(string ransomNote, string magazine) {
}
return true;
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -318,6 +318,43 @@ impl Solution {
}
```
Scala:
```scala
object Solution {
// 导包
import scala.collection.mutable
def fourSumCount(nums1: Array[Int], nums2: Array[Int], nums3: Array[Int], nums4: Array[Int]): Int = {
// 定义一个HashMapkey存储值value存储该值出现的次数
val map = new mutable.HashMap[Int, Int]()
// 遍历前两个数组把他们所有可能的情况都记录到map
for (i <- nums1.indices) {
for (j <- nums2.indices) {
val tmp = nums1(i) + nums2(j)
// 如果包含该值则对他的key加1不包含则添加进去
if (map.contains(tmp)) {
map.put(tmp, map.get(tmp).get + 1)
} else {
map.put(tmp, 1)
}
}
}
var res = 0 // 结果变量
// 遍历后两个数组
for (i <- nums3.indices) {
for (j <- nums4.indices) {
val tmp = -(nums3(i) + nums4(j))
// 如果map中存在该值结果就+=value
if (map.contains(tmp)) {
res += map.get(tmp).get
}
}
}
// 返回最终结果可以省略关键字return
res
}
}
C#
```csharp
public int FourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {