-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[回溯算法] Backtrack #6
Comments
回溯问题 = 决策树遍历过程
|
1 - 46. 全排列# 第一版
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
_len = len(nums)
if _len == 0: return []
if _len == 1: return [nums]
result = []
def select(selectedlist, todolist): # 已经选择 / 待选择
if len(todolist) == 0: # 可选择列表为空
result.append(selectedlist) # 将值直接返回, 结束
return
for i in range(len(todolist)): # 遍历选择
newselect = selectedlist + [todolist[i]] # 我做出的选择
newtodo = todolist[0:i] + todolist[i+1:] # 新的待选择
select(newselect, newtodo)
select([], nums) # 初始化执行 已选择列表为空, 待选择为整个排序
return result # 第二版
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
_len = len(nums)
if _len == 0: return []
if _len == 1: return [nums]
result = []
def backtrack (pos, nums):
if pos == _len:
result.append(nums[:]) # 不能直接写nums,会返回值都一样的, 深拷贝
return
for i in range(pos, _len):
nums[pos], nums[i] = nums[i], nums[pos] # 交换 选择
backtrack(pos + 1, nums)
nums[pos], nums[i] = nums[i], nums[pos] # 撤回 回溯
backtrack(0, nums) # 是通过交换位置, 不用每次都创建新的todolist
return result 同 面试题38. 字符串的排列 TODO
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: