39. 组合总和
python
复制代码
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
path = []
res = []
def dfs(candidates,target,s,index):
if s == target:
res.append(path[:])
return
for i in range(index,len(candidates)):
if s + candidates[i] > target:
break
s += candidates[i]
path.append(candidates[i])
dfs(candidates,target,s,i)
s -= candidates[i]
path.pop()
dfs(candidates,target,0,0)
return res
40. 组合总和 II
python
复制代码
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
used = [False for _ in range(len(candidates))]
path = []
res = []
def dfs(candidates,used,target,s,index):
if s == target:
res.append(path[:])
return
for i in range(index,len(candidates)):
if s + candidates[i] > target:
break
if i > 0 and candidates[i] ==candidates[i-1] and used[i-1] == False:
continue
s += candidates[i]
used[i] = True
path.append(candidates[i])
dfs(candidates,used,target,s,i+1)
s -= candidates[i]
used[i] =False
path.pop()
dfs(candidates,used,target,0,0)
return res
131. 分割回文串
python
复制代码
class Solution:
def partition(self, s: str) -> List[List[str]]:
def isornot(s,left,right):
if s[left:right+1] == s[left:right+1][::-1]:
return True
else:
return False
path = []
res = []
def dfs(s,index):
if index ==len(s):
res.append(path[:])
return
for i in range(index,len(s)):
if isornot(s,index,i):
path.append(s[index:i+1])
dfs(s,i+1)
path.pop()
dfs(s,0)
return res
93. 复原 IP 地址
python
复制代码
class Solution:
def isvalue(self,s,start,end):
if start > end:
return False
if s[start] =='0' and start != end:
return False
if not 0 <=int(s[start:end+1]) <= 255:
return False
return True
def restoreIpAddresses(self, s: str) -> List[str]:
self.result = []
self.dfs(s,0,0)
return self.result
def dfs(self,s,index,point_num):
if point_num ==3:
if self.isvalue(s,index,len(s)-1):
self.result.append(s[:])
return
for i in range(index,len(s)):
if self.isvalue(s,index,i):
s = s[:i+1] + '.' +s[i+1:]
point_num +=1
self.dfs(s,i+2,point_num)
point_num -=1
s = s[:i+1] + s[i+2:]
else:
break
78. 子集
python
复制代码
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
path = []
res = []
def dfs(nums,index):
res.append(path[:])
if index > len(nums)-1:return
for i in range(index,len(nums)):
path.append(nums[i])
dfs(nums,i+1)
path.pop()
dfs(nums,0)
return res
90. 子集 II
python
复制代码
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
nums.sort()
used = [False for i in range(len(nums))]
path =[]
res = []
def dfs(nums,used,index):
res.append(path[:])
if index == len(nums):
return
for i in range(index,len(nums)):
if i > 0 and nums[i-1] == nums[i] and used[i-1] ==False:
continue
path.append(nums[i])
used[i] = True
dfs(nums,used,i+1)
used[i] = False
path.pop()
dfs(nums,used,0)
return res
491. 非递减子序列
python
复制代码
class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
path = []
res = []
def dfs(nums,index):
if len(path) > 1:
res.append(path[:])
# if index > len(nums)-1:return
used = set()
for i in range(index,len(nums)):
if (path and path[-1] > nums[i]) or nums[i] in used:
continue
used.add(nums[i])
path.append(nums[i])
dfs(nums,i+1)
path.pop()
dfs(nums,0)
return res
46. 全排列
python
复制代码
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
path = []
res = []
used = [False for i in range(len(nums))]
def dfs(nums,used):
if len(path) == len(nums):
res.append(path[:])
return
for i in range(len(nums)):
if used[i] ==True:
continue
path.append(nums[i])
used[i] = True
dfs(nums,used)
path.pop()
used[i] = False
dfs(nums,used)
return res
47. 全排列 II
python
复制代码
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
nums.sort()
used = [False for i in range(len(nums))]
path = []
res = []
def dfs(nums,used):
if len(path) ==len(nums):
res.append(path[:])
return
for i in range(len(nums)):
if i > 0 and nums[i-1] == nums[i] and used[i-1] == False:
continue
if used[i] == True:
continue
path.append(nums[i])
used[i] = True
dfs(nums,used)
used[i] = False
path.pop()
dfs(nums,used)
return res