美文网首页
BackTracking

BackTracking

作者: inspiredhss | 来源:发表于2020-01-20 07:41 被阅读0次
  1. Letter Combinations of a Phone Number
# Letter Combinations of a Phone Number
# Given a string containing digits from 2-9 inclusive, 
# return all possible letter combinations represent.
# mapping of digit to letters is given.

class Solution:
    def letterCombinations(self, digits):
        if not digits:
            return []
        dic = {"2":"abc", "3":"def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
        res = []
        self.dfs(digits, dic, 0, "", res)
        return res

    def dfs(self, digits, dic, index, path, res):
        if len(path) == len(digits):
            res.append(path)  #深度搜索到的每条str
            return 
        for i in range(index, len(digits)):
            for j in dic[digits[i]]:  # 数字逐位 遍历深入 至串长达到
                self.dfs(digits, dic, i+1, path+j, res)  # index & strPath
  1. Word Search II
# Word Search II
# Given a 2D board and a list of words from the dictionary, 
# find all words in the board.

# sequentially adjacent cell, 
# "adjacent": horizontally or vertically neighboring. 
# same letter not be used more than once in a word.

class Solution:
    def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
        WORD_KEY = '$'
        
        trie = {}
        for word in words:
            node = trie
            for letter in word:
                # retrieve the next node; If not found, create a empty node.
                node = node.setdefault(letter, {})
            # mark the existence of a word in trie node
            node[WORD_KEY] = word
        
        rowNum = len(board)
        colNum = len(board[0])
        
        matchedWords = []
        
        def backtracking(row, col, parent):    
            
            letter = board[row][col]
            currNode = parent[letter]
            
            # check if we find a match of word
            word_match = currNode.pop(WORD_KEY, False)
            if word_match:
                # also we removed the matched word to avoid duplicates,
                #   as well as avoiding using set() for results.
                matchedWords.append(word_match)
            
            # Before the EXPLORATION, mark the cell as visited 
            board[row][col] = '#'
            
            # Explore the neighbors in 4 directions, i.e. up, right, down, left
            for (rowOffset, colOffset) in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
                newRow, newCol = row + rowOffset, col + colOffset     
                if newRow < 0 or newRow >= rowNum or newCol < 0 or newCol >= colNum:
                    continue
                if not board[newRow][newCol] in currNode:
                    continue
                backtracking(newRow, newCol, currNode)
        
            # End of EXPLORATION, we restore the cell
            board[row][col] = letter
        
            # Optimization: incrementally remove the matched leaf node in Trie.
            if not currNode:
                parent.pop(letter)

        for row in range(rowNum):
            for col in range(colNum):
                # starting from each of the cells
                if board[row][col] in trie:
                    backtracking(row, col, trie)
        
        return matchedWords    
  1. WildCard Matching
# WildCard Matching
# s; pattern p; wildcard pattern ? *
class Solution:
    def isMatch(self, s, p):
        length = len(s)
        if len(p) - p.count('*') > length:
            return False
        dp = [True] + [False]*length
        for i in p:
            if i != '*':
                for n in reversed(range(length)):
                    dp[n+1] = dp[n] and (i == s[n] or i == '?')
            else:
                for n in range(1, length+1):
                    dp[n] = dp[n-1] or dp[n]
            dp[0] = dp[0] and i == '*'
        return dp[-1]
  1. Regular Expression Matching
# 10. Regular Expression Matching
# Input: string (s) and a pattern (p)
# support for '.' and '*'
# Input:
# s = "aab"
# p = "c*a*b"
# Output: true
# Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".

class Solution:
    def isMatch(self, s: str, p: str) -> bool:
        dp = [[False] * (len(s) + 1) for _ in range(len(p) + 1)]
        dp[0][0] = True
        for i in range(1, len(p)):
            dp[i + 1][0] = dp[i - 1][0] and p[i] == '*'
        for i in range(len(p)):
            for j in range(len(s)):
                if p[i] == '*':
                    dp[i + 1][j + 1] = dp[i - 1][j + 1] or dp[i][j + 1]
                    if p[i - 1] == s[j] or p[i - 1] == '.':
                        dp[i + 1][j + 1] |= dp[i + 1][j]
                else:
                    dp[i + 1][j + 1] = dp[i][j] and (p[i] == s[j] or p[i] == '.')
        return dp[-1][-1]

相关文章

  • 回溯法

    backtracking in a glance 首先系统地介绍一下backtracking这个方法本质是建立在递...

  • backtracking

    http://blog.csdn.net/crystal6918/article/details/51924665...

  • Backtracking

    A general approach to backtracking questions in Java (Sub...

  • BackTracking

    回溯法的核心思想是当对一个数组第一个元素递归到最后一个字符的时候利用自底向上的方法,逐步删除每一个字符回到最初的状...

  • Backtracking

    Combination Sum Most basic backtracking. The idea of begi...

  • BackTracking

    Letter Combinations of a Phone Number Word Search II Wild...

  • zerojudge a229: 括號匹配問題

    原題目在zerojudge,若對於backtracking技術不熟可看演算法筆記-backtracking Pro...

  • 5. 回溯与分支

    What is backtracking algorithm? Backtraking is a general ...

  • [LeetCode] Backtracking

    Usually, the main idea of the so-called backtraking is to...

  • backtracking(回溯)

    回溯法,是通过递归的方式穷举组合问题的所有可能解,关于这个问题的解决方法大致有如下步骤 建立解矩阵,如果不知道可行...

网友评论

      本文标题:BackTracking

      本文链接:https://www.haomeiwen.com/subject/zsodzctx.html