프로그래머스27 [프로그래머스] 정수 내림차순으로 배치하기 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 import Foundation func solution(_ n:Int64) -> Int64 { return Int64(String(String(n).sorted(by: >)))! //return Int64(String(String(n).sorted{ $0 > $1 }))! } 다른 풀이 import Foundation func solution(_ n:Int64) -> Int64 { return Int64(String(String(n).sorted(by: >))) ?? 0 } 2024. 1. 5. [프로그래머스] 정수 제곱근 판별 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 제곱근 함수로 sqrt() 가 있다. 실수형이라서 Float 나 Double 형으로 import Foundation func solution(_ n:Int64) -> Int64 { var x = Int64(sqrt(Double(n))) if x * x != n { return -1 } else { return (x+1) * (x+1) } } 다른 풀이 삼항 연산자 활용 import Foundation func solution(_ n:Int64) -> Int64 { var x = Int64(sqrt(Double(n))) return x * x == n ? (x+1) * (x+1) : -1 } 2024. 1. 4. [프로그래머스] 두 정수 사이의 합 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 func solution(_ a:Int, _ b:Int) -> Int64 { var sum = 0 for i in (a > b ? b...a : a...b) { sum += i } return Int64(sum) } 다른 풀이 func solution(_ a:Int, _ b:Int) -> Int64 { return Int64((a > b ? b...a : a...b).reduce(0, +)) } 2023. 12. 22. [프로그래머스] 문자열을 정수로 바꾸기 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 func solution(_ s:String) -> Int { return Int(s)! } 2023. 12. 22. [프로그래머스] 자연수 뒤집어 배열로 만들기 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 입력받는 n 값을 String으로 변환 map을 이용해서 String을 모두 Int로 변환해 reversed func solution(_ n:Int64) -> [Int] { return String(n).map{ Int(String($0))! }.reversed() } 다른 풀이 // 2023. 12. 21. [프로그래머스] x만큼 간격이 있는 n개의 숫자 (Swift) - LV.1 ⬆ 문제 풀기 ⬆ 풀이 func solution(_ x:Int, _ n:Int) -> [Int64] { var array = [Int64]() for i in 1...n { array.append(Int64(x*i)) } return array } 다른 풀이 // 2023. 12. 20. 이전 1 2 3 4 5 다음