Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub
class Solution {
    fun isValid(s: String): Boolean {
        var deque = ArrayDeque<Char>()

        for (i in 0..s.length-1) {
            var character = s.get(i)
            println(character)
            if (character == '(' || character == '[' || character == '{')  
            {
                deque.push(character)
                continue; 
            }
            if (deque.isEmpty()) {
                return false; 
            }
            when (character) {
                ')' -> if (deque.getFirst() == '(') {deque.pop()} else { return false }
                ']' -> if (deque.getFirst() == '[') {deque.pop()} else { return false }
                '}' -> if (deque.getFirst() == '{') {deque.pop()} else { return false }
                else -> return false
            }
        }
        if(!deque.isEmpty()){
            return false
        }
        return true
    }
}