From abe00238f44718c49b9442c26e7f7b01f84be3e4 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 13 May 2022 16:37:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200001.=E4=B8=A4?= =?UTF-8?q?=E6=95=B0=E4=B9=8B=E5=92=8C.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 9571a773..141e66f3 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -274,6 +274,27 @@ 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) + } +} +``` -----------------------
From 53379c023fe935398801f41aad3dd0399b2d7d73 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 13 May 2022 17:28:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200454.=E5=9B=9B?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=8A=A0II.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0454.四数相加II.md | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/problems/0454.四数相加II.md b/problems/0454.四数相加II.md index a6cd413b..d4aba8fa 100644 --- a/problems/0454.四数相加II.md +++ b/problems/0454.四数相加II.md @@ -318,5 +318,41 @@ 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 = { + // 定义一个HashMap,key存储值,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 + } +} +``` -----------------------
From 16c6abff54ec4b85d0891ed12914f538da7b8332 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Fri, 13 May 2022 21:02:07 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 5d9e8295..8e4cbbf8 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -360,6 +360,68 @@ 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 + } +} +``` +版本二: 使用HashMap +```scala +object Solution { + import scala.collection.mutable + def canConstruct(ransomNote: String, magazine: String): Boolean = { + // 如果magazine的长度小于ransomNote的长度,必然是false + if (magazine.length < ransomNote.length) { + return false + } + // 定义map,key是字符,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 + } +} +``` -----------------------