프로그래머스 (Swift)/기초
[Day7-5] 배열 만들기 4
은더기
2024. 2. 17. 21:54
문제
내 정답 코드
import Foundation
func solution(_ arr:[Int]) -> [Int] {
var stk:[Int] = []
var i = 0
while i < arr.count {
if stk.isEmpty {
stk.append(arr[i])
i += 1
} else if stk.last! < arr[i] {
stk.append(arr[i])
i += 1
} else {
stk.removeLast()
}
}
return stk
}
#
1. 문제의 조건문 3개만 만족시켜주면 어렵지 않은 문제이다.
2. 배열의 마지막 요소를 removeLast()를 통해 삭제할 수 있다는 것을 알았다.
배운 기술
1. removeLast()
https://developer.apple.com/documentation/swift/array/removelast()
removeLast() | Apple Developer Documentation
Removes and returns the last element of the collection.
developer.apple.com
2. removeFirst()
https://developer.apple.com/documentation/swift/array/removefirst()
removeFirst() | Apple Developer Documentation
Removes and returns the first element of the collection.
developer.apple.com