프로그래머스 - 3진법 뒤집기
·
Algorithm/programmers
import Foundationfunc solution(_ n: Int) -> Int { var tmp = n var list: [String] = [] while tmp > 0 { list.append(String(tmp % 3)) tmp /= 3 } var result = 0 for (i, char) in list.reversed().enumerated() { if let digit = Int(String(char)) { result += digit * Int(pow(Double(3), Double(i))) } } return result}