목록swift 알고리즘/스택 (5)
쥐수의 공부노트

정답 : let n = Int(readLine()!)! var check = true var pos = 1 var result : [String] = [] var array : [Int] = [] for _ in 1...n { let num = Int(readLine()!)! while pos num { check = false } } } if check { result.forEach{print($0)} } else { print("NO") } 더보기 TMI : 실패가 떠서 이유를 찾아보니, print("no")를 해서 틀린 것.. 대소문자에 주의하자..

정답 : while true { var array : [Character] = [] let str = readLine()! if str == "." { break } else { for i in str { if i == "(" || i == ")" || i == "[" || i == "]" { if i == "]" && array.last == "[" { array.removeLast() } else if i == ")" && array.last == "(" { array.removeLast() } else { array.append(i) } } } } print(array.isEmpty == true ? "yes" : "no") } 더보기 TMI : 전에는 스택으로 풀지 않았지만, 이번엔 참고하여 스택..

정답 : let n = Int(readLine()!)! var pos = 0 var array : [Int] = [] var sum = 0 func push(num : Int) { array.append(num) pos += 1 } func pop() { array.remove(at: pos - 1) pos -= 1 } for _ in 0..
해당 문제는 다른 카테고리에서 미리 풀었던 문제입니다. https://jisssuu.tistory.com/164 백준 10828번 스택 정답 : let n = Int(readLine()!)! var array : [Int] = [] var pos = 0 func push(num:Int) { array.append(num) pos += 1 } func pop() { var result = -1 if pos != 0 { result = array[pos-1] } print(result) if result != -1 { array.remove(at: pos-1) pos -= 1 } } jisssuu.tistory.com 해당 사이트를 참고해주세요!