recursion
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res;
vector<int> cur;
helper(nums, 0, cur, res);
return res;
}
void helper(vector<int>& nums, int start, vector<int>& cur, vector<vector<int>>& res){
res.push_back(cur);
if(start>=nums.size()) return;
for(int i=start; i<nums.size();i++){
cur.push_back(nums[i]);
helper(nums, i+1, cur, res);
cur.pop_back();
}
}
};
iterative
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> res(1);
for(int i=0;i<nums.size();i++){
int size = res.size();
for(int j=0;j<size;j++){
res.push_back(res[j]);
res.back().push_back(nums[i]);
}
}
return res;
}
};
recursion
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
helper(nums, 0, res);
return res;
}
void helper(vector<int> cur, int start, vector<vector<int>>& res){
res.push_back(cur);
for(int i=start; i<cur.size()-1;i++){
for(int j=i+1; j<cur.size();j++){
swap(cur[i], cur[j]);
helper(cur, i+1, res);
swap(cur[i], cur[j]);
}
}
}
};
iterative
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = [[nums[0]]];
for j in range(1, len(nums)):
cur = [];
for arr in res:
for i in range(0, len(arr)+1):
cur.append(arr[:i]+[nums[j]]+arr[i:]);
res = cur;
return res;
recursion
class Solution:
def generateAbbreviations(self, word: str) -> List[str]:
res = [word]
self.helper(word, 0, res)
return res
def helper(self, word, pos, res):
for i in range(pos, len(word)):
for j in range(1, len(word)-i+1):
t = word[:i]+str(j)+word[i+j:]
res.append(t)
self.helper(t, i+1+len(str(j)), res)
iterative
利用binary,0->原数,1->数字
recursion
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
suffixDic = dict()
def dfs(s):
ans = []
if s in wordDict:
ans.append(s)
for i in range(len(s)-1):
prefix, suffix = s[:i+1], s[i+1:]
if prefix not in wordDict:
continue
rest = []
if suffix in suffixDic:
rest = suffixDic[suffix]
else:
rest = dfs(suffix)
for n in rest:
ans.append(prefix+" "+n)
suffixDic[s] = ans;
return ans;
return dfs(s)
recursion
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
def dfs(l, r, cur):
if l==r and l+r==2*n :
res.append(cur)
return
if l<n:
dfs(l+1, r, cur+"(")
if r<l:
dfs(l, r+1, cur+")")
dfs(1, 0, "(")
return res
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
def dfs(row, col, word, index):
if board[row][col] != word[index]:
return False
board[row][col] = '-'
if index+1==len(word):
return True
res = False
if row-1>=0:
res = dfs(row-1, col, word, index+1)
if col-1>=0:
res = res or dfs(row, col-1, word, index+1)
if row+1<len(board):
res = res or dfs(row+1, col, word, index+1)
if col+1<len(board[0]):
res = res or dfs(row, col+1, word, index+1)
board[row][col] = word[index]
return res
for i in range(len(board)):
for j in range(len(board[0])):
if dfs(i, j, word, 0):
return True
return False
17. Letter Combinations of a Phone Number
recursion
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if len(digits)==0: return []
numDict = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
res = []
def dfs(pos, cur):
if pos==len(digits):
res.append(cur)
return
for n in numDict[int(digits[pos])]:
dfs(pos+1, cur+n)
dfs(0, "")
return res
iterative
class Solution:
def letterCombinations(self, digits: str) -> List[str]:
if len(digits)==0: return []
numDict = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
res = [""]
for d in digits:
string = numDict[int(d)]
cur = []
for ss in res:
for s in string:
cur.append(ss+s)
res = cur
return res
recrusion
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
def dfs(target, cur, pos):
if target<0: return
if target==0:
res.append(cur)
return
for i in range(pos, len(candidates)):
dfs(target-candidates[i], cur+[candidates[i]], i)
dfs(target, [], 0)
return res