문제: https://leetcode.com/problems/valid-parentheses/
Valid Parentheses - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
import Foundation
class Solution {
func isValid(_ s: String) -> Bool {
var stack = [Character]()
for char in s {
if char == "(" || char == "{" || char == "[" {
stack.append(char)
} else {
if stack.isEmpty { return false }
let last = stack.removeLast()
if last == "(" && char == ")" {
continue
} else if last == "{" && char == "}" {
continue
} else if last == "[" && char == "]" {
continue
} else {
return false
}
}
}
return stack.count == 0
}
}
'알고리즘' 카테고리의 다른 글
[Swift] LeetCode - 3. Longest Substring Without Repeating Characters (0) | 2022.02.16 |
---|---|
[Swift] LeetCode - 641. Design Circular Deque (0) | 2022.02.16 |
[Swift] LeetCode - 2. Add Two Numbers (0) | 2022.02.15 |
[Swift] LeetCode - 49. Group Anagrams (0) | 2022.02.14 |
[Swift] LeetCode - 819. Most Common Word (0) | 2022.02.14 |
댓글