Leetcode Backtracking 题型总结(1)

在 Leetcode 看到很好的一篇总结,所以我根据看到的再重新写一篇,方便自己理解

首先理解一下什么是回溯,可以用迷宫来做比喻.
通常走出一个迷宫的方法如下:

  1. 进入迷宫(废话...)
  2. 选一条道一直走, 一直走到有一个分岔
  3. 选最左的一条道
  4. 如果你走到了死胡同, 就回到之前最后一个分岔口, 选从左边数起第二条分岔.
  5. 一直循环 2 - 5, 直到找到出口位置

伪代码就变成了这个样子:

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

接下来看 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 当成 DFS
    def subsets(self, S):
        self.result = []
        self.backtrack(0, sorted(S), [])
        return self.result

        # 这个是模板啊~
    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],
[]
]

这题就是多加了一个判断, 去掉重复的

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]: # 忽略重复的
                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]
]

跟前两道题的区别在于 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[:])
                # 由于长度一样, 所以不用知道 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]
]

一样玩法,查重复的

class DFS2:
    def permuteUnique(self, nums):
        self.result = []
        # 查重的方法改变了,通过一个数组来记录有没有走过这个位置的数
        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)):
                # 保证前一个相同的数也必须包含在结果里
                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, 原因是因为一个数可以重复利用很多次
                self.backtrack(candidates, target, i, total + candidates[i], temp)
                temp.pop()

题型变化一下, 一样是跟重复有关系
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()

接下来的题加了个限制总共可以用多少个数

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个数字

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, 包含了负数和不限制数量的条件,要用 DP 来解题, 这个以后在别的地方再提吧.
接下来的题其实都大同小异了...

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, 再加个回文的判定

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)

这道题的用法其实是一样的, 加上有效判断数字的话能快不少

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 超时了...这个题意很简单,就是要找到除了1和自己以外有因数的解, n <= 1 && temp.size() > 1这个条件可以把1 和自己本身的数去掉.

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);
                }
            }
        }
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,198评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,663评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,985评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,673评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,994评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,399评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,717评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,407评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,112评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,371评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,891评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,255评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,881评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,010评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,764评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,412评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,299评论 2 260

推荐阅读更多精彩内容