[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..
[Swift] Array์˜ ์š”์†Œ๋ฅผ key, value๋ฅผ ๊ฐ–๋Š” Dictionary๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ
ยท
๐Ÿ“ ์ฝ”ํ…Œ/๊ฟ€ํŒ
let array = [1, 1, 2, 4, 4, 4, 5] ์ด๋Ÿฐ ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ let dict = [1: 2, 2: 1, 4: 3, 5: 1] Array์˜ ์š”์†Œ๋ฅผ Dictionary๋กœ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด Dictionary์˜ ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ์ข‹๋‹ค. let dictionary = Dictionary(array.map { ($0, 1) }, uniquingKeysWith: +) ์ด๋Ÿฐ์‹์œผ๋กœ ์‰ฝ๊ฒŒ Dictionary๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
JerryiOS
'๐Ÿ“ ์ฝ”ํ…Œ/๊ฟ€ํŒ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก