Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17a818c

Browse files
Merge pull request youngyangyang04#1338 from wzqwtt/patch13
添加(0232.用栈实现队列、0225.用队列实现栈)Scala版本
2 parents bdb357e + f4d98a7 commit 17a818c

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

‎problems/0225.用队列实现栈.md‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,89 @@ class MyStack {
815815
}
816816
}
817817
```
818+
Scala:
818819

820+
使用两个队列模拟栈:
821+
```scala
822+
import scala.collection.mutable
823+
824+
class MyStack() {
825+
826+
val queue1 = new mutable.Queue[Int]()
827+
val queue2 = new mutable.Queue[Int]()
828+
829+
def push(x: Int) {
830+
queue1.enqueue(x)
831+
}
832+
833+
def pop(): Int = {
834+
var size = queue1.size
835+
// 将queue1中的每个元素都移动到queue2
836+
for (i <- 0 until size - 1) {
837+
queue2.enqueue(queue1.dequeue())
838+
}
839+
var res = queue1.dequeue()
840+
// 再将queue2中的每个元素都移动到queue1
841+
while (!queue2.isEmpty) {
842+
queue1.enqueue(queue2.dequeue())
843+
}
844+
res
845+
}
846+
847+
def top(): Int = {
848+
var size = queue1.size
849+
for (i <- 0 until size - 1) {
850+
queue2.enqueue(queue1.dequeue())
851+
}
852+
var res = queue1.dequeue()
853+
while (!queue2.isEmpty) {
854+
queue1.enqueue(queue2.dequeue())
855+
}
856+
// 最终还需要把res送进queue1
857+
queue1.enqueue(res)
858+
res
859+
}
860+
861+
def empty(): Boolean = {
862+
queue1.isEmpty
863+
}
864+
}
865+
```
866+
使用一个队列模拟:
867+
```scala
868+
import scala.collection.mutable
869+
870+
class MyStack() {
871+
872+
val queue = new mutable.Queue[Int]()
873+
874+
def push(x: Int) {
875+
queue.enqueue(x)
876+
}
877+
878+
def pop(): Int = {
879+
var size = queue.size
880+
for (i <- 0 until size - 1) {
881+
queue.enqueue(queue.head) // 把头添到队列最后
882+
queue.dequeue() // 再出队
883+
}
884+
queue.dequeue()
885+
}
886+
887+
def top(): Int = {
888+
var size = queue.size
889+
var res = 0
890+
for (i <- 0 until size) {
891+
queue.enqueue(queue.head) // 把头添到队列最后
892+
res = queue.dequeue() // 再出队
893+
}
894+
res
895+
}
896+
897+
def empty(): Boolean = {
898+
queue.isEmpty
899+
}
900+
}
901+
```
819902
-----------------------
820903
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

‎problems/0232.用栈实现队列.md‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,45 @@ void myQueueFree(MyQueue* obj) {
495495
obj->stackOutTop = 0;
496496
}
497497
```
498-
498+
Scala:
499+
```scala
500+
class MyQueue() {
501+
import scala.collection.mutable
502+
val stackIn = mutable.Stack[Int]() // 负责出栈
503+
val stackOut = mutable.Stack[Int]() // 负责入栈
504+
505+
// 添加元素
506+
def push(x: Int) {
507+
stackIn.push(x)
508+
}
509+
510+
// 复用代码,如果stackOut为空就把stackIn的所有元素都压入StackOut
511+
def dumpStackIn(): Unit = {
512+
if (!stackOut.isEmpty) return
513+
while (!stackIn.isEmpty) {
514+
stackOut.push(stackIn.pop())
515+
}
516+
}
517+
518+
// 弹出元素
519+
def pop(): Int = {
520+
dumpStackIn()
521+
stackOut.pop()
522+
}
523+
524+
// 获取队头
525+
def peek(): Int = {
526+
dumpStackIn()
527+
val res: Int = stackOut.pop()
528+
stackOut.push(res)
529+
res
530+
}
531+
532+
// 判断是否为空
533+
def empty(): Boolean = {
534+
stackIn.isEmpty && stackOut.isEmpty
535+
}
536+
}
537+
```
499538
-----------------------
500539
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /