Computer Science/Algorithm

Swift로 알고리즘 풀 때 알아둬야할 것들

kyxxn 2024. 7. 1. 01:19
728x90

1. Character 타입은 Int로 변환이 불가능하다.

let str = "12345"
let intValue: Int = Int(str[str.index(str.startIndex, offsetBy: 0)]))! // 에러
let intValue: Int = Int(String(str[str.index(str.startIndex, offsetBy: 0)]))! // 정답

 

2. pow 함수는 첫 번째 인자가 Decimal 타입인데, 그냥 Double 쓰면 됨

let a = 10
let b = 5
let result = Int(pow(Double(a), Double(b)))