File tree Expand file tree Collapse file tree 4 files changed +157
-0
lines changed
0022 Generate Parentheses Expand file tree Collapse file tree 4 files changed +157
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 0022. Generate Parentheses
2
+
3
+ * Difficulty: medium
4
+ * Link: https://leetcode.com/problems/generate-parentheses/
5
+ * Topics: Backtracking
6
+
7
+ # Clarification
8
+
9
+ 1 . Check the inputs and outputs
10
+ - INPUT:
11
+ - n: int
12
+ - OUTPUT: List[ String]
13
+
14
+ # Naive Solution
15
+
16
+ ### Thought Process
17
+
18
+ - Backtracking
19
+ - 停止條件
20
+ - open == n and close == n
21
+ - 非停止條件
22
+ - 當 open < n
23
+ - subset = subset + open
24
+ - 當 close < open
25
+ - subset = subest + close
26
+
27
+ ![ Untitled] ( ./Untitled.png )
28
+
29
+ - Implement
30
+
31
+ ``` python
32
+ class Solution (object ):
33
+ def generateParenthesis (self , n ):
34
+ """
35
+ :type n: int
36
+ :rtype: List[str]
37
+ """
38
+ result = []
39
+
40
+ def backtracking (substr , num_open , num_close ):
41
+ if num_open == n and num_close == n:
42
+ return result.append(substr)
43
+ if num_open < n:
44
+ backtracking(substr + " (" , num_open + 1 , num_close)
45
+ if num_close < num_open:
46
+ backtracking(substr + " )" , num_open, num_close+ 1 )
47
+
48
+ backtracking(" " , 0 ,0 )
49
+ return result
50
+ ```
51
+
52
+
53
+ # Note
54
+
55
+ - ** [Generate Parentheses - Stack - Leetcode 22 ](https:// www.youtube.com/ watch? v = s9fokUqJ76A)**
Original file line number Diff line number Diff line change
1
+ # 40. Combination Sum II
2
+
3
+ * Difficulty: medium
4
+ * Link: https://leetcode.com/problems/combination-sum-ii/
5
+ * Topics: Array-String, Backtracking
6
+
7
+ # Clarification
8
+
9
+ 1 . Check the inputs and outputs
10
+ - INPUT:
11
+ - canditates: List[ int]
12
+ - target: int
13
+ - OUTPUT: List[ List[ int]
14
+
15
+ # Naive Solution
16
+
17
+ ### Thought Process
18
+
19
+ - Backtracking
20
+ - 停止條件
21
+ - sum of subset == target : add to result
22
+ - sum of subset > target : return
23
+ - 未達停止條件
24
+ - subset + res[ i]
25
+ - res = res[ i+1:]
26
+ - 注意:當 res[ i] == res[ i+1] 時跳過,避免重複
27
+ - Implement
28
+
29
+ ``` python
30
+ class Solution (object ):
31
+ def combinationSum2 (self , candidates , target ):
32
+ """
33
+ :type candidates: List[int]
34
+ :type target: int
35
+ :rtype: List[List[int]]
36
+ """
37
+ candidates.sort()
38
+ result = []
39
+ def backtracking (subset , res ):
40
+ if sum (subset) == target:
41
+ return result.append(subset)
42
+ if sum (subset) > target:
43
+ return
44
+ for idx in range (len (res)):
45
+ if idx > 0 and res[idx] == res[idx- 1 ]:
46
+ continue
47
+ backtracking(subset + [res[idx]], res[idx+ 1 :])
48
+
49
+ backtracking([], candidates)
50
+ return result
51
+ ```
Original file line number Diff line number Diff line change
1
+ # 216. Combination Sum III
2
+
3
+ * Difficulty: medium
4
+ * Link: https://leetcode.com/problems/combination-sum-iii/
5
+ * Topics: Backtracking
6
+
7
+ # Clarification
8
+
9
+ 1 . Check the inputs and outputs
10
+ - INPUT:
11
+ - k: int
12
+ - n: int (target)
13
+ - OUTPUT: List [ List[ int] ]
14
+
15
+ # Naive Solution
16
+
17
+ ### Thought Process
18
+
19
+ - Backtracking
20
+ - 停止條件
21
+ - len(subset) == k and sum(subset) == n ⇒ add to result
22
+ - sum(subset) > n ⇒ return
23
+ - len(subset) > k ⇒ return
24
+ - 非停止條件
25
+ - subset = subset + res[ i]
26
+ - res = res[ i+1:]
27
+ - Implement
28
+
29
+ ``` python
30
+ class Solution (object ):
31
+ def combinationSum3 (self , k , n ):
32
+ """
33
+ :type k: int
34
+ :type n: int
35
+ :rtype: List[List[int]]
36
+ """
37
+ result = []
38
+ candidates = range (1 ,10 )
39
+ def backtracking (subset , res ):
40
+ if len (subset) == k and sum (subset) == n:
41
+ return result.append(subset)
42
+ if len (subset) > k:
43
+ return
44
+ if sum (subset) > n:
45
+ return
46
+ for idx, num in enumerate (res):
47
+ backtracking(subset + [res[idx]], res[idx+ 1 :])
48
+
49
+ backtracking([], candidates)
50
+ return result
51
+ ```
You can’t perform that action at this time.
0 commit comments