Skip to content
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

Open
Linjiayu6 opened this issue Jun 17, 2020 · 2 comments
Open

[回溯算法] Backtrack #6

Linjiayu6 opened this issue Jun 17, 2020 · 2 comments

Comments

@Linjiayu6
Copy link
Owner

No description provided.

@Linjiayu6
Copy link
Owner Author

回溯问题 = 决策树遍历过程

  1. 路径 做出选择
  2. 选择列表 你可以做的选择
  3. 结束条件 到达决策树底层 无法再做选择

@Linjiayu6
Copy link
Owner Author

Linjiayu6 commented Jun 17, 2020

1 - 46. 全排列

image

# 第一版
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

leetcode 讲解

# 第二版
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

输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]

@Linjiayu6 Linjiayu6 changed the title [回溯算法] [回溯算法] Backtrack Jun 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant