일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- C++
- ingress-nginx
- 방통대 대학원 정보과학과
- hackerrank
- Code Jam 2022
- 2022
- 하늘과 바람과 별과 시
- GitLab
- K8S
- 하늘과 바람과 별과 詩
- 코딩테스트
- Python
- 해커랭크
- Qualification Round
- openebs
- LEVEL 2
- 파이썬
- swift
- 프로그래머스
- Kubernetes
- 3D PRINTING
- secondlowestgrade
- nestedlists
- on-prem
- ESXi 업데이트
- 정보과학과
- 방송통신대학교 대학원 정보과학과
- Code Jam
- MySQL
Archives
- Today
- Total
공대생의 비망록
[프로그래머스][Lv. 1] 정수 내림차순으로 배치하기 Swift 풀이 본문
https://programmers.co.kr/learn/courses/30/lessons/12933
풀이는 추후에 차차 올리도록 하겠습니다...
주석 처리한 한 줄로 문제를 한번에 해결할 수 있으니 참고하면 좋습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
func solution(_ n:Int64) -> Int64 {
// return Int64(String(Array(String(n).sorted(by: {$0 > $1}))))!
var tmp: Int64 = n
var arr: [Int] = [Int]()
repeat {
arr.append(Int(tmp % 10))
tmp /= 10
} while tmp > 0
arr.sort(by: >)
var ans: Int64 = 0
let count = arr.count
if count > 1 {
for i in 0..<count-1 {
ans += Int64(arr[i])
ans *= 10
}
ans += Int64(arr[count-1])
} else {
ans = Int64(arr[0])
}
return ans
}
|
cs |
'Programming Language > Swift' 카테고리의 다른 글
[프로그래머스][Lv. 1] 자릿수 더하기 Swift 풀이 (0) | 2022.03.14 |
---|---|
[프로그래머스][Lv. 1] 자연수 뒤집어 배열로 만들기 Swift 풀이 (0) | 2022.03.14 |
[프로그래머스][Lv. 1] 정수의 제곱근 판별 Swift 풀이 (0) | 2022.03.14 |
[프로그래머스][Lv. 1] 제일 작은 수 제거하기 Swift 풀이 (0) | 2022.03.14 |
[프로그래머스][Lv. 1] 짝수와 홀수 Swift 풀이 (0) | 2022.03.14 |
Comments