mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #1343 from wzqwtt/patch15
添加(0150.逆波兰表达式求值、0239.滑动窗口最大值、0347.前K个高频元素)Scala版本
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
```
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -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
|
||||
}
|
||||
```
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
Reference in New Issue
Block a user