ํ๋ก๊ทธ๋๋จธ์ค - ๊ดํธ ํ์ ํ๊ธฐ
2022. 10. 12. 11:15ใํ๋ก๊ทธ๋๋จธ์ค-Swift
ํ๋ก๊ทธ๋๋จธ์ค - ๊ดํธ ํ์ ํ๊ธฐ
๋ฌธ์ ์ค๋ช
https://school.programmers.co.kr/learn/courses/30/lessons/76502
๋์ ํ์ด
์ด๋ฐ ๊ดํธ ๋ฌธ์ ๋ ์คํ์ด ๊ฑฐ์ ๊ณ ์ ์ธ ๊ฒ ๊ฐ๋ค.
์ฌ๋ ๋์ ๊ฐ์ด removeFirst์ removeLast๋ฅผ ์ฌ์ฉํด์ ๋ฌธ์ ๋ฅผ ํ์๋ค.
import Foundation
func solution(_ s:String) -> Int {
var result = 0
var changedArr = Array(s).map{ String($0) }
var stack: [String] = []
for i in 0..<s.count {
for j in changedArr {
if stack.isEmpty {
stack.append(j)
} else {
if stack.last! == "(" && j == ")" || stack.last! == "{" && j == "}" || stack.last! == "[" && j == "]" {
stack.removeLast()
} else {
stack.append(j)
}
}
}
result += stack.isEmpty ? 1 : 0
stack = []
changedArr.append(changedArr.removeFirst())
}
return result
}
'ํ๋ก๊ทธ๋๋จธ์ค-Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค - n^2 ๋ฐฐ์ด ์๋ฅด๊ธฐ (0) | 2022.10.13 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค - ํํ(Swift) (0) | 2022.10.12 |
ํ๋ก๊ทธ๋๋จธ์ค - ํ๋ ฌ์ ๊ณฑ์ (Swift) (0) | 2022.10.12 |
ํ๋ก๊ทธ๋๋จธ์ค - h-Index(Swift) (0) | 2022.10.11 |
์บ์ - ํ๋ก๊ทธ๋๋จธ์ค(Swift) (0) | 2022.10.11 |