Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c203be

Browse files
author
hj.tian
committed
feat: add leetcode75 day35
feat: add leetcode75 day35
1 parent 5bf1117 commit 0c203be

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

‎leetcode75/day35_39.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
7+
"""
8+
动态规划
9+
10+
2 3 6 7
11+
0
12+
1
13+
2
14+
3
15+
4
16+
5
17+
6
18+
7
19+
20+
{
21+
0: [[]]
22+
2: [[] + [2]] = [[2]]
23+
4: [[2] + [2]] = [[2,2]]
24+
6: [[2,2] + [2], [3]+[3], []+[6]] = [[2,2.2], [3,3], [6]]
25+
3: [[] + [3]] [[3]]
26+
5: [[2] + [3]]
27+
7: [[2,2] + [3], [0] + [7]] = [[2,2,3], [7]]
28+
}
29+
"""
30+
dp = defaultdict(list)
31+
dp[0] = [[]]
32+
for candidate in candidates:
33+
for num in range(1, target + 1):
34+
if num >= candidate and dp[num - candidate]:
35+
dp[num].extend([case + [candidate] for case in dp[num - candidate]])
36+
return dp[target]
37+
38+
39+
# class Solution:
40+
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
41+
# """
42+
# 搜索 + 剪枝
43+
# 7
44+
# -2 -3 -6 -7
45+
# -2 -3 -6 -7
46+
# -2 -3 -6 -7
47+
# -2 -3 -6 -7
48+
# """
49+
# self.ans = []
50+
# candidates.sort()
51+
# self.dfs(candidates, 0, target, [])
52+
# return self.ans
53+
54+
# def dfs(self, candidates, index, target, path):
55+
# if target == 0:
56+
# self.ans.append(path)
57+
# return
58+
# for i in range(index, len(candidates)):
59+
# if target - candidates[i] < 0:
60+
# break
61+
# # self.dfs(candidates, i, target - candidates[i], path + [candidates[i]])
62+
# target -= candidates[i]
63+
# pre_path = [num for num in path]
64+
# path.append(candidates[i])
65+
# self.dfs(candidates, i, target, path)
66+
# target += candidates[i]
67+
# path = pre_path
68+
69+
70+
if __name__ == "__main__":
71+
assert Solution().combinationSum(candidates=[2, 3, 6, 7], target=7) == [[2, 2, 3], [7]]
72+
assert Solution().combinationSum(candidates=[2, 3, 5], target=8) == [[2, 2, 2, 2], [2, 3, 3], [3, 5]]
73+
assert Solution().combinationSum(candidates=[2], target=1) == []

‎leetcode75/day35_46.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from itertools import permutations
2+
from typing import List
3+
4+
5+
class Solution:
6+
def permute(self, nums: List[int]) -> List[List[int]]:
7+
"""4. 使用 itertools"""
8+
return [list(item) for item in permutations(nums)]
9+
10+
11+
# class Solution:
12+
# def permute(self, nums: List[int]) -> List[List[int]]:
13+
# """3. 搜索+
14+
15+
# 1 2 3
16+
# 2 3
17+
# 3 2
18+
# """
19+
# self.ans = []
20+
# visit = [False for _ in nums]
21+
# self.dfs(nums, visit, [])
22+
# return self.ans
23+
24+
# def dfs(self, nums, visit, item):
25+
# if len(item) == len(nums):
26+
# self.ans.append(item)
27+
# return
28+
# for i in range(len(nums)):
29+
# if visit[i] is False:
30+
# visit[i] = True
31+
# self.dfs(nums, visit, item + [nums[i]])
32+
# visit[i] = False
33+
34+
35+
# class Solution:
36+
# def permute(self, nums: List[int]) -> List[List[int]]:
37+
# """2. 迭代
38+
# 先解决子问题, 推导出大问题
39+
# dp[0] = [[1]]
40+
# # dp[i] 是前一项 dp[i-1] 每个 item 的每个位置插入 nums[i]
41+
# dp[1] = [
42+
# item[:index] + nums[1] + item[index:]
43+
# for item in dp[0]
44+
# for index in range(len(item) + 1)
45+
# ]
46+
# """
47+
# # dp = [[] for _ in nums]
48+
# # dp[0] = [[nums[0]]]
49+
# # for i in range(1, len(nums)):
50+
# # for sub in dp[i - 1]:
51+
# # for j in range(len(sub) + 1):
52+
# # dp[i].append(sub[:j] + [nums[i]] + sub[j:])
53+
# # return dp[len(nums) - 1]
54+
55+
# # 空间优化, dp[i] 只与 dp[i-1]相关
56+
# dp_0 = [[nums[0]]]
57+
# for i in range(1, len(nums)):
58+
# dp_1 = []
59+
# for sub in dp_0:
60+
# for j in range(len(sub) + 1):
61+
# dp_1.append(sub[:j] + [nums[i]] + sub[j:])
62+
# dp_0 = dp_1
63+
# return dp_0
64+
65+
66+
# class Solution:
67+
# def permute(self, nums: List[int]) -> List[List[int]]:
68+
# """1. 递归
69+
# 要解决大问题,先解决子问题
70+
# 如果我解决了 permute(nums[1:]), 如何得到 premute(nums) 的答案呢?
71+
# for sub in permute(nums[1:]):
72+
# for i in range(len(sub) + 1):
73+
# sub[:i] + nums[0] + sub[i:]
74+
# 即将 nums[0] 插在每个item的每个位置
75+
# """
76+
# if len(nums) == 1:
77+
# return [[nums[0]]]
78+
# per_permute = self.permute(nums[1:])
79+
# ans = []
80+
# for sub in per_permute:
81+
# for i in range(len(sub) + 1):
82+
# ans.append(sub[:i] + [nums[0]] + sub[i:])
83+
# return ans
84+
85+
86+
if __name__ == "__main__":
87+
assert sorted(Solution().permute(nums=[1, 2, 3])) == sorted(
88+
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
89+
)
90+
assert sorted(Solution().permute(nums=[0, 1])) == sorted([[0, 1], [1, 0]])
91+
assert Solution().permute(nums=[1]) == [[1]]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /