Leetcode Backtracking 題型總結(jié)(1)

在 Leetcode 看到很好的一篇總結(jié),所以我根據(jù)看到的再重新寫(xiě)一篇,方便自己理解

首先理解一下什么是回溯,可以用迷宮來(lái)做比喻.
通常走出一個(gè)迷宮的方法如下:

  1. 進(jìn)入迷宮(廢話...)
  2. 選一條道一直走, 一直走到有一個(gè)分岔
  3. 選最左的一條道
  4. 如果你走到了死胡同, 就回到之前最后一個(gè)分岔口, 選從左邊數(shù)起第二條分岔.
  5. 一直循環(huán) 2 - 5, 直到找到出口位置

偽代碼就變成了這個(gè)樣子:

start
while exist not found:
  for each path in the fork:
    check whether the path is safe
    if yes :
      select it and continue walk
  if all paths don't resolve the maze, return False
end

接下來(lái)看 Leetcode 原題

Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

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

class DFS:  # 也可以把 backtracking 當(dāng)成 DFS
    def subsets(self, S):
        self.result = []
        self.backtrack(0, sorted(S), [])
        return self.result

        # 這個(gè)是模板啊~
    def backtrack(self, start, S, temp):
        self.result.append(temp[:])
        for i in range(start , len(S)):
            temp.append(S[i])
            self.backtrack(i + 1, S, temp)
            temp.pop()

看 follow up:

Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

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

這題就是多加了一個(gè)判斷, 去掉重復(fù)的

class DFS:
    def subsetsWithDup(self, S):
        self.result = []
        if len(S) == 0: return self.result
        self.helper(0, sorted(S), [])
        return self.result

    def backtrack(self, start, S, temp):
        self.result.append(temp[:])
        for i in range(start, len(S)):
            if i != start and S[i] == S[i - 1]: # 忽略重復(fù)的
                continue
            temp.append(S[i])
            self.helper(i + 1, S, temp)
            temp.pop()

另外一類相似的題

Permutations

Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

跟前兩道題的區(qū)別在于 subset 可以不包括全部.

class Solution:
    def permute(self, nums):
        self.result = []
        self.backtrack(nums, [])
        return self.result

    def backtrack(self, nums, temp):
        if len(temp) == len(nums):
            self.result.append(temp[:])
                # 由于長(zhǎng)度一樣, 所以不用知道 index,連 i 都免了
        else:
            for n in nums:
                if n in temp:
                    continue
                temp.append(n)
                self.backtrack(nums, temp)
                temp.pop()

Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[
[1,1,2],
[1,2,1],
[2,1,1]
]

一樣玩法,查重復(fù)的

class DFS2:
    def permuteUnique(self, nums):
        self.result = []
        # 查重的方法改變了,通過(guò)一個(gè)數(shù)組來(lái)記錄有沒(méi)有走過(guò)這個(gè)位置的數(shù)
        self.backtrack(sorted(nums), [], [False for _ in range(len(nums))])
        return self.result

    def backtrack(self, nums, temp, visited):
        if len(nums) == len(temp):
            self.result.append(temp[:])
        else:
            for i in range(len(nums)):
                # 保證前一個(gè)相同的數(shù)也必須包含在結(jié)果里
                if visited[i] or (i > 0 and nums[i] == nums[i - 1]) and not visited[i - 1]:
                    continue
                visited[i] = True
                temp.append(nums[i])
                self.backtrack(nums, temp, visited)
                visited[i] = False
                temp.pop()

另一組題目

Combination Sum

Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

The same repeated number may be chosen from C unlimited number of times.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
[
[7],
[2, 2, 3]
]

class DFS:
    # @param candidates, a list of integers
    # @param target, integer
    # @return a list of lists of integers
    def combinationSum(self, candidates, target):
        self.result= []
        self.backtrack(sorted(candidates), target, 0, 0, [])
        return self.result

    def backtrack(self, candidates, target, start, total, temp):
        if total > target:
            return
        elif total == target:
            self.result.append(temp[:])
        else:
            for i in range(start, len(candidates)):
                temp.append(candidates[i])
                # 這里注意用的是 i 而不是 i+ 1, 原因是因?yàn)橐粋€(gè)數(shù)可以重復(fù)利用很多次
                self.backtrack(candidates, target, i, total + candidates[i], temp)
                temp.pop()

題型變化一下, 一樣是跟重復(fù)有關(guān)系
Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

Each number in C may only be used once in the combination.

Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]

class Solution:
    # @param candidates, a list of integers
    # @param target, integer
    # @return a list of lists of integers
    def combinationSum2(self, candidates, target):
        self.result = []
        self.backtrack(sorted(candidates), 0, 0, target, [])
        return self.result

    def backtrack(self, candidates, start, total, target, temp):
        if total > target:
            return
        elif total == target:
            self.result.append(temp[:])
        else:
            for i in range(start, len(candidates)):
                if i > start and candidates[i] == candidates[i - 1]:
                    continue
                temp.append(candidates[i])
                self.backtrack(candidates, i + 1, candidates[i] + total, target, temp)
                temp.pop()

接下來(lái)的題加了個(gè)限制總共可以用多少個(gè)數(shù)

Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Example 1:

Input: k = 3, n = 7

Output:

[[1,2,4]]

Example 2:

Input: k = 3, n = 9

Output:

[[1,2,6], [1,3,5], [2,3,4]]

要注意題目限制是 1 - 9, 9個(gè)數(shù)字

class Solution(object):
    def combinationSum3(self, k, n):
        """
        :type k: int
        :type n: int
        :rtype: List[List[int]]
        """
        self.result = []
        if k < 0:   return []
        self.backtrack(k, n, 1, 0, [])
        return self.result

    def backtrack(self, k, n, start, total, temp):
        if total == n and len(temp) == k:
            self.result.append(temp[:])
        else:
            for i in range(start, 10):
                temp.append(i)
                self.backtrack(k, n, i + 1, i + total, temp)
                temp.pop()

至于第四題 Combination Sum IV, 包含了負(fù)數(shù)和不限制數(shù)量的條件,要用 DP 來(lái)解題, 這個(gè)以后在別的地方再提吧.
接下來(lái)的題其實(shí)都大同小異了...

Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

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

變成了 String, 再加個(gè)回文的判定

class Solution(object):
    # @param s, a string
    # @return a list of lists of string
    def partition(self, s):
        self.result = []
        # track each possible partition
        self.backtrack(s, 0, [])
        return self.result

    def backtrack(self, s, start, temp):
        if start == len(s):
            self.result.append(temp[:])
        else:
            for i in range(start, len(s)):
                string = s[start: i + 1]
                if self.isPalindrome(string):
                    temp.append(string)
                    self.backtrack(s, i + 1, temp)
                    temp.pop()

    def isPalindrome(self, string):
        left, right = 0, len(string) - 1
        while left < right:
            if string[left] != string[right]:
                return False
            left+=1
            right-=1
        return True

Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

這道題的用法其實(shí)是一樣的, 加上有效判斷數(shù)字的話能快不少

class Solution:
    # @param s, a string
    # @return a list of strings
    def restoreIpAddresses(self, s):
        if len(s) < 4 or len(s) > 12:
            return []
        self.result = []
        self.backtrack(s, 0, [])
        return self.result

    def backtrack(self, s, start, temp):
        if len(temp) == 4 and start == len(s):
            self.result.append(".".join(temp))
        for i in range(start, min(start + 4, len(s))):
            string = s[start: i + 1]
            if self.isValid(string):
                temp.append(string)
                self.backtrack(s, i + 1, temp)
                temp.pop()

    def isValid(self, string):
        if not string:  return False
        if len(string) > 1 and string[0] == '0':
            return False
        return int(string) <= 255

Factor Combinations

Numbers can be regarded as product of its factors. For example,

8 = 2 x 2 x 2;
= 2 x 4.
Write a function that takes an integer n and return all possible combinations of its factors.

Note:
You may assume that n is always positive.
Factors should be greater than 1 and less than n.
Examples:
input: 1
output:
[]
input: 37
output:
[]
input: 12
output:
[
[2, 6],
[2, 2, 3],
[3, 4]
]
input: 32
output:
[
[2, 16],
[2, 2, 8],
[2, 2, 2, 4],
[2, 2, 2, 2, 2],
[2, 4, 4],
[4, 8]
]

相同的算法但是 Python 超時(shí)了...這個(gè)題意很簡(jiǎn)單,就是要找到除了1和自己以外有因數(shù)的解, n <= 1 && temp.size() > 1這個(gè)條件可以把1 和自己本身的數(shù)去掉.

import java.util.List;
import java.util.ArrayList;

public class Solution {

    public static void main(String[] args) {
        System.out.println(new Solution().getFactors(8));
    }

    private List<List<Integer>> result;

    public List<List<Integer>> getFactors(int n) {
        result = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        backtrack(n, temp, 2);
        return result;
    }

    private void backtrack(int n, List<Integer> temp, int start){
        if (n <= 1 && temp.size() > 1) {
            result.add(new ArrayList<Integer>(temp));
        }else {
            for (int i = start; i < n + 1; i++) {
                if (n % i == 0) {
                    temp.add(i);
                    backtrack(n/i, temp, i);
                    temp.remove(temp.size() - 1);
                }
            }
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容