File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ // ํด์ค ์ฐธ์กฐ..
2+ class Solution {
3+ func combinationSum( _ candidates: [ Int ] , _ target: Int ) -> [ [ Int ] ] {
4+ // ์ฐ์ ๋ต๋ณ์ฉ, ์กฐํฉ ์ฐพ๊ธฐ์ฉ ๋ณ์ ์์ฑ
5+ var answer = [ [ Int] ] ( )
6+ var nums = [ Int] ( )
7+ // ๋ฐฑํธ๋ํน ๊ธฐ๋ฒ์ผ๋ก ๋ก์ง ์์ฑ,
8+ // ํ์ฌ ์์ ์์น์ ํฉํ์๋์ ๊ฐ์ ์ธ์๋ก ๋ฐ์
9+ func backtracking( start: Int , total: Int ) {
10+ // ์ ์ฒ๋ฆฌ
11+ if total > target { return } // total์ด target ๋ณด๋ค ํฌ๋ฉด ์กฐํฉX
12+ else if total == target { return answer. append ( nums) } // ๊ฐ์ผ๋ฉด ๋ต๋ณ์ฉ ๋ณ์์ ์ถ๊ฐ
13+
14+ // ์์ ๋ถ๋ถ๋ถํฐ ๊ฐ์ ํ๋์ฉ ๋ํด์ ์ฌ๊ท๋ก ๋๋ ค๋ด
15+ for index in start..< candidates. count {
16+ let temp = candidates [ index]
17+ nums. append ( temp) //๋จผ์ ์ ํ๋ ์์๋ฅผ ์กฐํฉ ๋ฐฐ์ด์ ์ถ๊ฐ
18+ backtracking ( start: index, total: total + temp) // ํ์ฌ ์ ํ๋ ์์์ ์ธ๋ฑ์ค์ ์ด ํฉ์ ์ธ์๋ก ํจ์ ํธ์ถ
19+ nums. removeLast ( ) // ์กฐํฉ์ฐพ๊ธฐ๊ฐ ๋๋๋ฉด ์ข
๋ฃ
20+ }
21+ }
22+ // ์ด๊ธฐ๋ถํฐ ์์ํจ
23+ backtracking ( start: 0 , total: 0 )
24+ return answer
25+ }
26+ }
You canโt perform that action at this time.
0 commit comments