Skip to content

Commit

Permalink
add backtracking
Browse files Browse the repository at this point in the history
  • Loading branch information
selfboot committed May 8, 2016
1 parent ab0527a commit ea290f5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Backtracking/216_CombinationSumIII.py
Expand Up @@ -22,12 +22,12 @@ def _combination_sum(self, k, n, nums):
# Get the new num from start
start = 1
if nums:
start = nums[-1]+1
start = nums[-1] + 1
for i in range(start, 10):
cur_sum = sum(nums) + i
if cur_sum <= n:
nums.append(i)
self._combination_sum(k-1, n, nums)
self._combination_sum(k - 1, n, nums)
del nums[-1] # Backtracking
else:
break
Expand Down
4 changes: 2 additions & 2 deletions Backtracking/46_Permutations.py
Expand Up @@ -13,7 +13,7 @@ def dfs(self, nums, path, ans):
if not nums:
ans.append(path)
for i, n in enumerate(nums):
self.dfs(nums[:i]+nums[i+1:], path+[n], ans)
self.dfs(nums[:i] + nums[i + 1:], path + [n], ans)


class Solution_2(object):
Expand All @@ -22,7 +22,7 @@ class Solution_2(object):
def permute(self, nums):
return [[n] + p
for i, n in enumerate(nums)
for p in self.permute(nums[:i] + nums[i+1:])] or [[]]
for p in self.permute(nums[:i] + nums[i + 1:])] or [[]]

"""
[]
Expand Down
7 changes: 4 additions & 3 deletions Week07/51.py → Backtracking/51_NQueens.py
Expand Up @@ -58,7 +58,8 @@ def isValid(self, row, col, matrix, n):
return True

"""
if __name__ == '__main__':
sol = Solution()
print sol.solveNQueens(2)
0
1
5
"""
14 changes: 4 additions & 10 deletions Backtracking/79_WordSearch.py
Expand Up @@ -3,13 +3,7 @@


class Solution(object):

def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
if not board and word:
return False
if not word:
Expand Down Expand Up @@ -66,13 +60,13 @@ def adj_pos_lists(self, cur_pos, board):
col = cur_pos[1]
adj_list = []
if row - 1 >= 0:
adj_list.append([row-1, col])
adj_list.append([row - 1, col])
if row + 1 < m_rows:
adj_list.append([row+1, col])
adj_list.append([row + 1, col])
if col - 1 >= 0:
adj_list.append([row, col-1])
adj_list.append([row, col - 1])
if col + 1 < n_cols:
adj_list.append([row, col+1])
adj_list.append([row, col + 1])
return adj_list

"""
Expand Down
4 changes: 2 additions & 2 deletions Backtracking/93_RestoreIPAddresses.py
Expand Up @@ -31,7 +31,7 @@ def restoreAddress(self, s, count):

# Current field is '0'
if s[0] == "0":
address_1 = self.restoreAddress(s[1:], count+1)
address_1 = self.restoreAddress(s[1:], count + 1)
for block in address_1:
cur_address = ['0']
cur_address.extend(block)
Expand All @@ -43,7 +43,7 @@ def restoreAddress(self, s, count):
for i in range(1, 4):
if len(s) < i or int(s[:i]) > 255:
continue
address_1 = self.restoreAddress(s[i:], count+1)
address_1 = self.restoreAddress(s[i:], count + 1)
for block in address_1:
cur_address = [s[:i]]
cur_address.extend(block)
Expand Down
9 changes: 2 additions & 7 deletions Backtracking/README.md
Expand Up @@ -49,13 +49,11 @@
* 如何保存根结点到叶子结点的路径;
* 如何回退到父结点。

针对不同的问题场景,上面的问题有不同的解决办法,无法一一给出。不过我们可以抽象地给出所有问题的一般解过程,可分为递归实现和迭代实现。

递归实现(保存所有的解):
针对不同的问题场景,上面的问题有不同的解决办法,无法一一给出。不过我们可以抽象地给出所有问题的一般解过程,下面为其递归实现(保存所有的解):

void backtrack(state s) {
if(s is end){ // 当前结点为可行解
sols.append(path) // 保存该解
sols.append(path); // 保存该解
}
else if(s has no ways){ // 当前结点为不可达叶子结点
return;
Expand All @@ -69,9 +67,6 @@
}
}

所有的递归实现均可以转换为迭代实现,只不过会用到额外的空间。回溯法迭代实现(保存所有的解)如下:


# 例子:更好的理解


Expand Down

0 comments on commit ea290f5

Please sign in to comment.