![JAR search and dependency download from the Maven repository](/logo.png)
g0901_1000.s0946_validate_stack_sequences.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g0901_1000.s0946_validate_stack_sequences
// #Medium #Array #Stack #Simulation #2023_04_30_Time_180_ms_(74.91%)_Space_40.7_MB_(6.18%)
import java.util.Deque
import java.util.LinkedList
class Solution {
fun validateStackSequences(pushed: IntArray, popped: IntArray): Boolean {
val stack: Deque = LinkedList()
var i = 0
var j = 0
val len = pushed.size
while (i < len) {
if (pushed[i] == popped[j]) {
i++
j++
} else if (stack.isNotEmpty() && stack.peek() == popped[j]) {
stack.pop()
j++
} else {
stack.push(pushed[i++])
}
}
while (j < len) {
if (stack.isNotEmpty() && stack.peek() != popped[j++]) {
return false
} else {
stack.pop()
}
}
return true
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy