306. 累加数

17. 电话号码的字母组合

22. 括号生成

93. 复原IP地址

131. 分割回文串

46. 全排列

47. 全排列 II

77. 组合

39. 组合总和

40. 组合总和 II

# 306. 累加数

class Solution(object):
"""
:type num: str
:rtype: bool
"""
n, flag = len(num), False
#num[:i + 1], num[i + 1: j + 1], num[j+1:.]
for i in range((n + 1) // 2):
if num[0] == '0' and i > 0:
break
for j in range(i + 1, n):
if num[i + 1] == '0' and j > i + 1:
break
a_2, a_1 = int(num[: i + 1]), int(num[i + 1 : j + 1])
start, local_ans = j, 2
while True:
if start >= n - 1:
break
a = a_1 + a_2

# print(i, j, a_2, a_1, a)
str_a = str(a)
m = len(str_a)
end = start + m
cur = int(num[start + 1: min(n, end + 1)])
if cur != a:
break
local_ans += 1
a_2 = a_1
a_1 = a
a = a_1 + a_2
start = end
if start == n - 1:
if local_ans >= 3:
return True
break
return False

# 17. 电话号码的字母组合

https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/

class Solution(object):
def __init__(self):
self.res = []

def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return self.res

rec = {"2": "abc", "3": "def", "4": "ghi",
"5": "jkl", "6": "mno", "7": "pqrs",
"8": "tuv", "9": "wxyz"}

self.DFS(0, "", digits, rec)
return self.res

def DFS(self, idx, tmp_res, digits, rec):
if idx >= len(digits):
self.res.append(tmp_res)
return

# for i in xrange(idx, len(digits)):
for j in xrange(0, len(rec[digits[idx]])):
self.DFS(idx + 1, tmp_res + rec[digits[idx]][j], digits, rec)

# 22. 括号生成

https://leetcode-cn.com/problems/generate-parentheses/

"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

if cnt == 0:
self.res.append(tmp_res)

if cnt <= 0:
self.DFS(cnt + 1, tmp_res + "(", n - 1)
elif cnt > n:
self.DFS(cnt - 1, tmp_res + ")", n - 1)
class Solution(object):
def __init__(self):
self.res = []

def generateParenthesis(self, n):
"""
:type n: int
:rtype: List[str]
"""
if n <= 0:
return []
self.DFS(0, "", 2 * n)
return self.res

def DFS(self, cnt, tmp_res, n):
if n == 0:
if cnt == 0:
self.res.append(tmp_res)
return

if cnt <= 0:
self.DFS(cnt + 1, tmp_res + "(", n - 1)
elif cnt > n:
self.DFS(cnt - 1, tmp_res + ")", n - 1)
else:
self.DFS(cnt - 1, tmp_res + ")", n - 1)
self.DFS(cnt + 1, tmp_res + "(", n - 1)

# 93. 复原IP地址

• 到达最后一个字符串
• 如果符合条件，前面已将添加过3个'.'，且最后一段也符合IP地址的格式，在[0,255]范围，且第一个字符为0只能是最后一段为0，且只有一个字符，添加进res
• 如果不符合条件，直接返回
• 不符合条件，已经添加了不止3个'.'，范围不再[0,255]，或者第一个字符为'0'，但是这一段却不为0，或者这一段为0，字符数却不止一个。
class Solution(object):
def __init__(self):
self.res = []

"""
:type s: str
:rtype: List[str]
"""
if not s or len(s) > 12:
return self.res

self.helper(0, 0, 0, "", s)
return self.res

# before - 分隔符起始位置
def helper(self, before, idx, cnt, tmp_res, s):
if cnt > 3:
return
if idx == len(s) - 1:
num = int(s[before:])
if ((0 < num <= 255 and s[before] != '0') or (num == 0 and before == idx)) and cnt == 3:
self.res.append(tmp_res + s[-1])
return
num = int(s[before: idx + 1])
if (before - idx + 1 > 3 or not (0 <= num <= 255)
or (num ==0 and before != idx) or (num != 0 and s[before] == '0')):
return
self.helper(before, idx + 1, cnt, tmp_res + s[idx], s)
self.helper(idx + 1, idx + 1, cnt + 1, tmp_res + s[idx] +'.', s)

# 131. 分割回文串

https://leetcode-cn.com/problems/palindrome-partitioning/

[["aa","b"],
["a","a","b"]]

class Solution(object):
def __init__(self):
self.res = []

def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
if not s:
return self.res
self.helper(0, 0, [], s)
return self.res

def helper(self, before, idx, tmp_res, s):
if idx >= len(s):
return
if idx == len(s) - 1:
if self.isValid(s[before:]):
tmp_res.append(s[before:])
self.res.append(tmp_res[:])
tmp_res.pop()
return

if self.isValid(s[before: idx + 1]):
tmp_res.append(s[before: idx + 1])
self.helper(idx + 1, idx + 1, tmp_res, s)
tmp_res.pop()
self.helper(before, idx + 1, tmp_res, s)

def isValid(self, s):
l, r = 0, len(s) - 1
while l < r:
if s[l] != s[r]:
return False
l += 1
r -= 1
return True

class Solution(object):
def __init__(self):
self.res = []
self.dp = []

def partition(self, s):
if not s:
return self.res
self.dp = [[False] * len(s) for _ in range(len(s))]
for right in xrange(len(s)):
for left in xrange(right + 1):
if (s[left] == s[right] and (right - left <= 2 or self.dp[left + 1][right - 1])):
self.dp[left][right] = True

self.helper(0, 0, [], s)
return self.res

def helper(self, before, idx, tmp_res, s):
if idx >= len(s):
return
if idx == len(s) - 1:
if self.dp[before][idx]:
tmp_res.append(s[before:])
self.res.append(tmp_res[:])
tmp_res.pop()
return

if self.dp[before][idx]:
tmp_res.append(s[before: idx + 1])
self.helper(idx + 1, idx + 1, tmp_res, s)
tmp_res.pop()
self.helper(before, idx + 1, tmp_res, s)

# 46. 全排列

https://leetcode-cn.com/problems/permutations/

[ [1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]]

class Solution(object):
def __init__(self):
self.res = []

def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return self.res
used = [False] * len(nums)
self.helper([], used, nums)
return self.res

def helper(self, tmp_res, used, nums):
if len(tmp_res) == len(nums):
self.res.append(tmp_res[:])
return
for i in xrange(len(nums)):
if not used[i]:
tmp_res.append(nums[i])
used[i] = True
self.helper(tmp_res, used, nums)
tmp_res.pop()
used[i] = False

# 47. 全排列 II

https://leetcode-cn.com/problems/permutations-ii/

[[1,1,2],
[1,2,1],
[2,1,1]]

class Solution(object):
def __init__(self):
self.res = []

def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if not nums:
return self.res
nums = sorted(nums)
used = [False] * len(nums)
self.helper([], used, nums)
return self.res

def helper(self, tmp_res, used, nums):
if len(tmp_res) == len(nums):
self.res.append(tmp_res[:])
return

for i in range(len(nums)):
if i != 0 and nums[i] == nums[i - 1] and not used[i - 1]:
continue
if not used[i]:
tmp_res.append(nums[i])
used[i] = True
self.helper(tmp_res, used, nums)
tmp_res.pop()
used[i] = False

# 77. 组合

https://leetcode-cn.com/problems/combinations/

class Solution(object):
def __init__(self):
self.res = []

def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
if n <= 0 or k > n:
return self.res
self.DFS([], 0, n, k)
return self.res

def DFS(self, tmp_res, start, n, k):
if len(tmp_res) == k:
self.res.append(tmp_res[:])
return
start += 1
if start > n:
return

for i in xrange(start, n + 1):
tmp_res.append(i)
self.DFS(tmp_res, i, n, k)
tmp_res.pop()

# 39. 组合总和

https://leetcode-cn.com/problems/combination-sum/

[[7],
[2,2,3]]

[[2,2,2,2],
[2,3,3],
[3,5]]

class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
def DFS(start, tmp_res, target):
if target < 0:
return
if target == 0:
res.append(tmp_res[:])
return
for i in xrange(start, len(candidates)):
residue = target - candidates[i]
if residue < 0:
continue
tmp_res.append(candidates[i])
DFS(i, tmp_res, residue)
tmp_res.pop()

if not candidates or target <= 0:
return self.res
res = []
DFS(0, [], target)
return res

# 40. 组合总和 II

https://leetcode-cn.com/problems/combination-sum-ii/

[[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]]

[[1,2,2],
[5]]

class Solution(object):
def combinationSum2(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
def DFS(start, tmp_res, target):
if target < 0:
return
if target == 0:
res.append(tmp_res[:])
return
for i in xrange(start + 1, len(candidates)):
residue = target - candidates[i]
if residue < 0:
break
if i >= 1 and candidates[i] == candidates[i - 1] and not used[i - 1]:
continue
tmp_res.append(candidates[i])
used[i] = True
DFS(i, tmp_res, residue)
tmp_res.pop()
used[i] = False

candidates = sorted(candidates)
used = [False] * len(candidates)
res = []
DFS(-1, [], target)
return res

09-26

05-20 9699

12-07 134

07-30 431

01-13

05-17 349

08-21 381

07-09 465

11-22 272

06-02 1万+

05-08 107

06-01 103

04-30 56

10-25 19

05-17 100

08-24 155

05-19 1万+

06-23 123

05-11 3万+

04-09 3万+

11-27 1380

05-18 9133

10-30 567

04-11 4万+

05-19 6821

05-12 5498

04-12 113

04-22 2万+

05-13 7981

05-13 1万+

#### 动不动问原理，面试官你来说个ZooKeeper原理试试？

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试