[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..
[Swift] 순열, 조합
·
📝 코테/꿀팁
순열 func permutation(array: [T], n: Int) { var visited = [Bool](repeating: false, count: array.count) func permutation(current: [T]) { if current.count == n { print(current) return } for i in 0..
JerryiOS
'순열' 태그의 글 목록