[Swift] 순열과 조합 간단히 구하기
·
📝 코테/꿀팁
import Foundation let arr = [1, 2, 3] let n = 3 // O(n!) func perm(_ current: [Int]) { if current.count > n { return } arr.forEach { perm(current + [$0]) } if current.count < n { return } print(current) } perm([]) // O(2^n) func comb(_ current: [Int], _ idx: Int) { guard current.count < n else { print(current) return } for i in idx..