Movatterモバイル変換


[0]ホーム

URL:


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

Commit25d75fb

Browse files
authored
Merge pull requestneetcode-gh#3206 from themlkang42/0155-min-stack-kotlin
Update 0155-min-stack.kt
2 parentse80f705 +598e394 commit25d75fb

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

‎kotlin/0155-min-stack.kt‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
importkotlin.math.min
2-
31
classMinStack() {
4-
privateclassNode(var `val`:Int,varmin:Int,varnext:Node?)
5-
6-
privatevar head:Node?=null
7-
8-
funpush(x:Int) {
9-
if(head==null) head=Node(x,x,null)else head=Node(x, min(x,head?.min!!),head)
2+
privateval stack=Stack<Int>()
3+
privateval minStack=Stack<Int>()
4+
5+
funpush(`val`:Int) {
6+
val currentMin=if (minStack.isNotEmpty()) minStack.peek()elseInteger.MAX_VALUE
7+
val newMin= minOf(currentMin, `val`)
8+
stack.push(`val`)
9+
minStack.push(newMin)
1010
}
1111

1212
funpop() {
13-
head= head?.next
14-
13+
if (stack.isNotEmpty()) {
14+
stack.pop()
15+
minStack.pop()
16+
}
1517
}
1618

17-
funtop():Int? {
18-
return head?.`val`
19-
19+
funtop():Int {
20+
return stack.peek()
2021
}
2122

22-
fungetMin():Int? {
23-
return head?.min
24-
23+
fungetMin():Int {
24+
return minStack.peek()
2525
}
26-
2726
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp