@@ -147,8 +147,6 @@ class Solution:
147147 return result
148148```
149149
150-
151-
152150### [ permutations-ii] ( https://leetcode-cn.com/problems/permutations-ii/ )
153151
154152> 给定一个可包含重复数字的序列,返回所有不重复的全排列。
@@ -186,15 +184,159 @@ class Solution:
186184 return result
187185```
188186
187+ ### [ combination-sum] ( https://leetcode-cn.com/problems/combination-sum/ )
188+
189+ ``` Python
190+ class Solution :
191+ def combinationSum (self , candidates : List[int ], target : int ) -> List[List[int ]]:
192+
193+ n = len (candidates)
194+ result = []
195+
196+ def backtrack (first = 0 , route = [], route_sum = 0 ):
197+
198+ if route_sum == target:
199+ result.append(route.copy())
200+ return
201+
202+ if route_sum > target:
203+ return
204+
205+ for i in range (first, n):
206+ route.append(candidates[i])
207+ route_sum += candidates[i]
208+ backtrack(i, route, route_sum)
209+ route_sum -= route.pop()
210+
211+ return
212+
213+ backtrack()
214+ return result
215+ ```
216+
217+ ### [ letter-combinations-of-a-phone-number] ( https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ )
218+
219+ ``` Python
220+ class Solution :
221+ def letterCombinations (self , digits : str ) -> List[str ]:
222+
223+ n = len (digits)
224+ result = []
225+
226+ if n == 0 :
227+ return result
228+
229+ num2char = {
230+ ' 2' : [' a' , ' b' , ' c' ],
231+ ' 3' : [' d' , ' e' , ' f' ],
232+ ' 4' : [' g' , ' h' , ' i' ],
233+ ' 5' : [' j' , ' k' , ' l' ],
234+ ' 6' : [' m' , ' n' , ' o' ],
235+ ' 7' : [' p' , ' q' , ' r' , ' s' ],
236+ ' 8' : [' t' , ' u' , ' v' ],
237+ ' 9' : [' w' , ' x' , ' y' , ' z' ]
238+ }
239+
240+ def backtrack (idx = 0 , route = []):
241+ if idx == n:
242+ result.append(' ' .join(route))
243+ return
244+
245+ for c in num2char[digits[idx]]:
246+ route.append(c)
247+ backtrack(idx + 1 , route)
248+ route.pop()
249+
250+ return
251+
252+ backtrack()
253+ return result
254+ ```
255+
256+ ### [ palindrome-partitioning] ( https://leetcode-cn.com/problems/palindrome-partitioning/ )
257+
258+ ``` Python
259+ class Solution :
260+ def partition (self , s : str ) -> List[List[str ]]:
261+
262+ n = len (s)
263+ is_Pal = {}
264+
265+ def isPal (i , j ):
266+ if i < j:
267+ return is_Pal[i, j]
268+ return True
269+
270+ for i in range (n - 2 , - 1 , - 1 ):
271+ for j in range (i + 1 , n):
272+ is_Pal[i, j] = s[i] == s[j] and isPal(i + 1 , j - 1 )
273+
274+ result = []
275+
276+ def backtrack (left = - 1 , right = - 1 , route = []):
277+
278+ if not isPal(left, right):
279+ return
280+
281+ if right == n - 1 :
282+ result.append(route.copy())
283+ return
284+
285+ left = right + 1
286+ for i in range (left, n):
287+ route.append(s[left:i + 1 ])
288+ backtrack(left, i, route)
289+ route.pop()
290+
291+ return
292+
293+ backtrack()
294+ return result
295+ ```
296+
297+ ### [ restore-ip-addresses] ( https://leetcode-cn.com/problems/restore-ip-addresses/ )
298+
299+ ``` Python
300+ class Solution :
301+ def restoreIpAddresses (self , s : str ) -> List[str ]:
302+
303+ n = len (s)
304+ result = []
305+
306+ if n > 12 :
307+ return result
308+
309+ def Valid_s (i , j ):
310+ return i < j and j <= n and ((s[i] != ' 0' and int (s[i:j]) < 256 ) or (s[i] == ' 0' and i == j - 1 ))
311+
312+ def backtrack (start = 0 , route = []):
313+
314+ if len (route) == 3 :
315+ if Valid_s(start, n):
316+ result.append(' .' .join(route) + ' .' + s[start:])
317+ return
318+
319+ for i in range (start, start + 3 ):
320+ if Valid_s(start, i + 1 ):
321+ route.append(s[start:i + 1 ])
322+ backtrack(i + 1 , route)
323+ route.pop()
324+
325+ return
326+
327+ backtrack()
328+ return result
329+ ```
330+
331+
332+
189333## 练习
190334
191335- [ ] [ subsets] ( https://leetcode-cn.com/problems/subsets/ )
192336- [ ] [ subsets-ii] ( https://leetcode-cn.com/problems/subsets-ii/ )
193337- [ ] [ permutations] ( https://leetcode-cn.com/problems/permutations/ )
194338- [ ] [ permutations-ii] ( https://leetcode-cn.com/problems/permutations-ii/ )
195339
196- 挑战题目
197-
198340- [ ] [ combination-sum] ( https://leetcode-cn.com/problems/combination-sum/ )
199341- [ ] [ letter-combinations-of-a-phone-number] ( https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ )
200342- [ ] [ palindrome-partitioning] ( https://leetcode-cn.com/problems/palindrome-partitioning/ )
0 commit comments