[Swift] 프로그래머스154539 뒤에 있는 큰 수 찾기
·
📝 코테/프로그래머스
import Foundation func solution(_ numbers: [Int]) -> [Int] { var stack = [Int]() var res = Array(repeating: -1, count: numbers.count) for i in 0..
[Swift] 스택 구현해보기
·
💻 CS/자료구조
Stack LIFO : 마지막으로 들어온 놈이 첫번째로 나가는 자료구조 struct Stack { private var stack: [T] = [] public var count: Int { return stack.count } public var isEmpty: Bool { return stack.isEmpty } public mutating func push(_ element: T) { stack.append(element) } public mutating func pop() -> T? { return isEmpty ? nil : stack.popLast() } } 사용 var myStack = Stack() myStack.push(10) myStack.pop() 시간복잡도는 O(1)이다. 그런데 S..
[Swift] 백준9012 괄호
·
📝 코테/BOJ
import Foundation /// 기본 VPS : () /// 만일 x가 VPS라면 (x)도 VPS /// x: VPS, y: VPS라면 xy도 VPS let T = Int(readLine()!)! for _ in 1...T { let str = readLine()! if str.isVPS { print("YES") } else { print("NO") } } extension String { var isVPS: Bool { var stack = [Character]() for char in self { if char == "(" { stack.append(char) } else if char == ")" { if stack.isEmpty { return false // 첫글자에 ")"가 온경우 }..
[Swift] 백준10773
·
📝 코테/BOJ
import Foundation let K = Int(readLine()!)! var stack = [Int]() for _ in 1...K { let inputNum = Int(readLine()!)! if inputNum == 0 && !stack.isEmpty { stack.removeLast() } else { stack.append(inputNum) } } print(stack.reduce(0, +))
JerryiOS
'스택' 태그의 글 목록