๋ฐ์ํ
๐ ๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/92335
๐ ๋ฌธ์ ํ์ด
1. k์ง์๋ก ๋ฐ๊พธ๋ฉด์ ๋์์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ฃผ๋๋ฐ, 0์ผ๋ก split ํด์ ๋ฃ์ด์ฃผ๊ธฐ
2. splitํด์ ๋ฃ์ด์ค ๋ฐฐ์ด์์ ํ๋์ฉ ๊บผ๋ด์ ์์ ํ๋ณ
- 0์ split ๋จ์ด์ด๊ธฐ ๋๋ฌธ์ pass
- 1์ ์์๊ฐ ์๋๊ธฐ ๋๋ฌธ์ pass
- ์์ ํ๋ณํด์ค์ ์์๋ผ๋ฉด true, ์๋๋ผ๋ฉด false
3. ์์์ด๋ฉด count += 1 ํด์ค
๐ ์์ค์ฝ๋
import Foundation
func solution(_ n: Int, _ k: Int) -> Int {
let changeN = changeFormat(n: n, k: k)
var count = 0
for char in changeN {
if char == 0 {
continue
}
if char == 1 {
continue
}
if isPrime(n: char) {
count += 1
}
}
return count
}
func isPrime(n: Int) -> Bool {
if(n<4) {
return n == 1 ? false : true
}
for i in 2...Int(sqrt(Double(n))) {
if(n % i == 0) { return false }
}
return true
}
func changeFormat(n: Int, k: Int) -> [Int] {
let split = String(n, radix: k).split(separator: "0").map { Int($0)! }
return split
}
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] (Swift) Kakao - ์์ ์ต๋ํ (Lv.2) (0) | 2022.09.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] (Swift) Kakao - ํ๋ณดํค (Lv.2) (0) | 2022.08.31 |
[ํ๋ก๊ทธ๋๋จธ์ค] (Swift) kakao - ์คํจ์จ (2019 KAKAO BLIND RECRUITMENT) (0) | 2022.08.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] (Swift) Kakao - ๋ฉ๋ด ๋ฆฌ๋ด์ผ (0) | 2022.05.12 |
[ํ๋ก๊ทธ๋๋จธ์ค] (Swift) Kakao ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (0) | 2022.05.10 |