๋ฐ์ํ
์์ด
func permutation<T>(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..<array.count {
if !visited[i] {
visited[i] = true
permutation(current: current + [array[i]])
visited[i] = false
}
}
}
permutation(current: [])
}
์กฐํฉ
func combination<T>(array: [T], n: Int) {
func combination(index: Int, current: [T]) {
if current.count == n {
print(current)
return
}
for i in index..<array.count {
combination(index: i + 1, current: current + [array[i]])
}
}
combination(index: 0, current: [])
}
์ถ์ฒ : let us Go! ๋ง๋์ค๋
๋ฐ์ํ
'๐ ์ฝํ > ๊ฟํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ์์ด๊ณผ ์กฐํฉ ๊ฐ๋จํ ๊ตฌํ๊ธฐ (0) | 2023.05.09 |
---|---|
[Swift] Array์ ์์๋ฅผ key, value๋ฅผ ๊ฐ๋ Dictionary๋ก ๋ณํํ๊ธฐ (0) | 2023.04.11 |