๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

์ ์ ˆํ•œ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์™€ ์กฐํ•ฉ์„ ๊ตฌํ•ด์„œ ์ค‘๋ณต ๊ฑฐ๋ฅด๊ณ , ์†Œ์ˆ˜ ์ฐพ์œผ๋ฉด ๋

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/42839#


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

import Foundation
func isPrime(_ n: Int) -> Bool {
if n < 2 { // 2๋ณด๋‹ค ์ž‘์œผ๋ฉด ์†Œ์ˆ˜๊ฐ€ ์•„๋‹˜
return false
}
for i in 2..<n {
if n % i == 0 {
return false
}
}
return true
}
func solution(_ numbers:String) -> Int {
var picked = [String]()
var results = Set<Int>()
var nums = Array(numbers).map { String($0) }
let m = nums.count
var visited = Array(repeating: false, count: m+1)
/*
n: ์ „์ฒด ์›์†Œ์˜ ์ˆ˜
toPick: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ์˜ ์ˆ˜
*/
func pick(n: Int, toPick: Int) {
// ๊ธฐ์ € ์‚ฌ๋ก€: ๊ณ ๋ฅผ ์ˆ˜ ์žˆ๋Š” ์›์†Œ๊ฐ€ ์—†์œผ๋ฉด ์ข…๋ฃŒ
if toPick == 0 {
results.insert(Int(picked.joined())!)
return
}
// ์›์†Œ ๊ณ ๋ฅด๊ธฐ
for i in 0..<n {
if visited[i] {
continue
}
visited[i] = true
picked.append(String(nums[i]))
pick(n: n, toPick: toPick - 1)
picked.removeLast()
visited[i] = false
}
}
for i in 1...m {
pick(n: m, toPick: i)
}
var answer = 0
for re in results {
if isPrime(re) {
answer += 1
}
}
return answer
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

๋Œ“๊ธ€์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.