Merge pull request #1320 from wzqwtt/patch06

添加(1002.查找常用字符、0349.两个数组的交集、0202.快乐数)Scala版本
This commit is contained in:
程序员Carl
2022-06-02 08:38:04 +08:00
committed by GitHub
3 changed files with 111 additions and 1 deletions

View File

@ -386,6 +386,39 @@ bool isHappy(int n){
} }
``` ```
Scala:
```scala
object Solution {
// 引入mutable
import scala.collection.mutable
def isHappy(n: Int): Boolean = {
// 存放每次计算后的结果
val set: mutable.HashSet[Int] = new mutable.HashSet[Int]()
var tmp = n // 因为形参是不可变量,所以需要找到一个临时变量
// 开始进入循环
while (true) {
val sum = getSum(tmp) // 获取这个数每个值的平方和
if (sum == 1) return true // 如果最终等于 1则返回true
// 如果set里面已经有这个值了说明进入无限循环可以返回false否则添加这个值到set
if (set.contains(sum)) return false
else set.add(sum)
tmp = sum
}
// 最终需要返回值直接返回个false
false
}
def getSum(n: Int): Int = {
var sum = 0
var tmp = n
while (tmp != 0) {
sum += (tmp % 10) * (tmp % 10)
tmp = tmp / 10
}
sum
}
C# C#
```csharp ```csharp
public class Solution { public class Solution {

View File

@ -313,6 +313,50 @@ int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* re
} }
``` ```
Scala:
正常解法:
```scala
object Solution {
def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
// 导入mutable
import scala.collection.mutable
// 临时Set用于记录数组1出现的每个元素
val tmpSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
// 结果Set存储最终结果
val resSet: mutable.HashSet[Int] = new mutable.HashSet[Int]()
// 遍历nums1把每个元素添加到tmpSet
nums1.foreach(tmpSet.add(_))
// 遍历nums2如果在tmpSet存在就添加到resSet
nums2.foreach(elem => {
if (tmpSet.contains(elem)) {
resSet.add(elem)
}
})
// 将结果转换为Array返回return可以省略
resSet.toArray
}
}
```
骚操作1:
```scala
object Solution {
def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
// 先转为Set然后取交集最后转换为Array
(nums1.toSet).intersect(nums2.toSet).toArray
}
}
```
骚操作2:
```scala
object Solution {
def intersection(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
// distinct去重然后取交集
(nums1.distinct).intersect(nums2.distinct)
}
}
C#: C#:
```csharp ```csharp
public int[] Intersection(int[] nums1, int[] nums2) { public int[] Intersection(int[] nums1, int[] nums2) {
@ -330,6 +374,7 @@ C#:
} }
return one; return one;
} }
``` ```
## 相关题目 ## 相关题目

View File

@ -418,6 +418,38 @@ char ** commonChars(char ** words, int wordsSize, int* returnSize){
return ret; return ret;
} }
``` ```
Scala:
```scala
object Solution {
def commonChars(words: Array[String]): List[String] = {
// 声明返回结果的不可变List集合因为res要重新赋值所以声明为var
var res = List[String]()
var hash = new Array[Int](26) // 统计字符出现的最小频率
// 统计第一个字符串中字符出现的次数
for (i <- 0 until words(0).length) {
hash(words(0)(i) - 'a') += 1
}
// 统计其他字符串出现的频率
for (i <- 1 until words.length) {
// 统计其他字符出现的频率
var hashOtherStr = new Array[Int](26)
for (j <- 0 until words(i).length) {
hashOtherStr(words(i)(j) - 'a') += 1
}
// 更新hash取26个字母最小出现的频率
for (k <- 0 until 26) {
hash(k) = math.min(hash(k), hashOtherStr(k))
}
}
// 根据hash的结果转换输出的形式
for (i <- 0 until 26) {
for (j <- 0 until hash(i)) {
res = res :+ (i + 'a').toChar.toString
}
}
res
}
}
```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>