From 8c394c9f8588a1e94250dc93bede24b7380996f8 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 17 May 2022 17:08:11 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200150.=E9=80=86?= =?UTF-8?q?=E6=B3=A2=E5=85=B0=E8=A1=A8=E8=BE=BE=E5=BC=8F=E6=B1=82=E5=80=BC?= =?UTF-8?q?.md=20Scala=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0150.逆波兰表达式求值.md | 29 ++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md index fd3d69aa..47da06f6 100644 --- a/problems/0150.逆波兰表达式求值.md +++ b/problems/0150.逆波兰表达式求值.md @@ -325,6 +325,33 @@ func evalRPN(_ tokens: [String]) -> Int { return stack.last! } ``` - +Scala: +```scala +object Solution { + import scala.collection.mutable + def evalRPN(tokens: Array[String]): Int = { + val stack = mutable.Stack[Int]() // 定义栈 + // 抽取运算操作,需要传递x,y,和一个函数 + def operator(x: Int, y: Int, f: (Int, Int) => Int): Int = f(x, y) + for (token <- tokens) { + // 模式匹配,匹配不同的操作符做什么样的运算 + token match { + // 最后一个参数 _+_,代表x+y,遵循Scala的函数至简原则,以下运算同理 + case "+" => stack.push(operator(stack.pop(), stack.pop(), _ + _)) + case "-" => stack.push(operator(stack.pop(), stack.pop(), -_ + _)) + case "*" => stack.push(operator(stack.pop(), stack.pop(), _ * _)) + case "/" => { + var pop1 = stack.pop() + var pop2 = stack.pop() + stack.push(operator(pop2, pop1, _ / _)) + } + case _ => stack.push(token.toInt) // 不是运算符就入栈 + } + } + // 最后返回栈顶,不需要加return关键字 + stack.pop() + } +} +``` -----------------------
From 748a728420be62c835866e7027171462ef90f888 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 17 May 2022 18:41:52 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200239.=E6=BB=91?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3=E6=9C=80=E5=A4=A7=E5=80=BC.md=20Sca?= =?UTF-8?q?la=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index f269450f..eb32fdd2 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -630,6 +630,53 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] { return result } ``` +Scala: +```scala +import scala.collection.mutable.ArrayBuffer +object Solution { + def maxSlidingWindow(nums: Array[Int], k: Int): Array[Int] = { + var len = nums.length - k + 1 // 滑动窗口长度 + var res: Array[Int] = new Array[Int](len) // 声明存储结果的数组 + var index = 0 // 结果数组指针 + val queue: MyQueue = new MyQueue // 自定义队列 + // 将前k个添加到queue + for (i <- 0 until k) { + queue.add(nums(i)) + } + res(index) = queue.peek // 第一个滑动窗口的最大值 + index += 1 + for (i <- k until nums.length) { + queue.poll(nums(i - k)) // 首先移除第i-k个元素 + queue.add(nums(i)) // 添加当前数字到队列 + res(index) = queue.peek() // 赋值 + index+=1 + } + // 最终返回res,return关键字可以省略 + res + } +} + +class MyQueue { + var queue = ArrayBuffer[Int]() + + // 移除元素,如果传递进来的跟队头相等,那么移除 + def poll(value: Int): Unit = { + if (!queue.isEmpty && queue.head == value) { + queue.remove(0) + } + } + + // 添加元素,当队尾大于当前元素就删除 + def add(value: Int): Unit = { + while (!queue.isEmpty && value > queue.last) { + queue.remove(queue.length - 1) + } + queue.append(value) + } + + def peek(): Int = queue.head +} +``` -----------------------
From 47c4cc102123b1b43f09b7b766fdd2f1e1b979a9 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Tue, 17 May 2022 19:52:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200347.=E5=89=8DK?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=20Scala?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 1d6a358b..20932b28 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -374,7 +374,49 @@ function topKFrequent(nums: number[], k: number): number[] { }; ``` +Scala: +解法一: 优先级队列 +```scala +object Solution { + import scala.collection.mutable + def topKFrequent(nums: Array[Int], k: Int): Array[Int] = { + val map = mutable.HashMap[Int, Int]() + // 将所有元素都放入Map + for (num <- nums) { + map.put(num, map.getOrElse(num, 0) + 1) + } + // 声明一个优先级队列,在函数柯里化那块需要指明排序方式 + var queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.fromLessThan((x, y) => x._2 > y._2)) + // 将map里面的元素送入优先级队列 + for (elem <- map) { + queue.enqueue(elem) + if(queue.size > k){ + queue.dequeue // 如果队列元素大于k个,出队 + } + } + // 最终只需要key的Array形式就可以了,return关键字可以省略 + queue.map(_._1).toArray + } +} +``` +解法二: 相当于一个wordCount程序 +```scala +object Solution { + def topKFrequent(nums: Array[Int], k: Int): Array[Int] = { + // 首先将数据变为(x,1),然后按照x分组,再使用map进行转换(x,sum),变换为Array + // 再使用sort针对sum进行排序,最后take前k个,再把数据变为x,y,z这种格式 + nums.map((_, 1)).groupBy(_._1) + .map { + case (x, arr) => (x, arr.map(_._2).sum) + } + .toArray + .sortWith(_._2 > _._2) + .take(k) + .map(_._1) + } +} +``` -----------------------