↓快轉到主要內容
  1. LeetCode/

LeetCode 93: Restore IP Addresses

·1 分鐘· ·
LeetCode Medium String Backtracking
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2022-11-05

  • 提交紀錄:Accepted ×2

解題思路
#

切割型回溯(Backtracking 變化五):每一層切出 1 到 3 個字元,合法的條件是 0 <= int(piece) <= 255 而且 piece == "0" or piece[0] != "0"。切滿 4 段時剛好用完字串才記錄。剪枝:剩下的字元數要在 剩下段數 × 1 到 剩下段數 × 3 之間。

我的嘗試
#

兩次提交都 AC(2022-11),第一次的寫法沒有限制每段最多 3 個字元,靠 int(piece) < 256 擋掉太長的片段,也沒有用剩餘長度剪枝。

正確解法
#

class Solution:
    def restoreIpAddresses(self, s: str) -> List[str]:
        res, path = [], []
        def dfs(start):
            left = 4 - len(path)
            if left == 0:
                if start == len(s):
                    res.append(".".join(path))
                return
            if not left <= len(s) - start <= 3 * left:
                return
            for end in range(start + 1, min(start + 3, len(s)) + 1):
                piece = s[start:end]
                if (piece == "0" or piece[0] != "0") and int(piece) <= 255:
                    path.append(piece)
                    dfs(end)
                    path.pop()
        dfs(0)
        return res

複雜度
#

時間 O(3^4),空間 O(1)(不算輸出)。

要特別避免的錯誤
#

  • 前導零:"01" 不合法,但 "0" 合法。
  • 必須剛好 4 段,而且用完整個字串。

面試口說整理
#

我用切割型的回溯,每段切 1 到 3 個字元,值不超過 255、除了 0 本身不能有前導零,切滿四段剛好用完字串就記錄;剩下的長度不可能分完時就剪掉。

相關文章

LeetCode 131: Palindrome Partitioning
·1 分鐘
LeetCode Medium String Dynamic-Programming Backtracking
LeetCode 131 解題筆記:每一層從剩下字串的開頭切出一段回文,切的長度從 1 到整段都要試
LeetCode 79: Word Search
·2 分鐘
LeetCode Medium Array String Backtracking Depth-First-Search
LeetCode 79 解題筆記:從每一格開始回溯,往四個方向比對下一個字元,走過的格子暫時標記,回溯時還原;先用字元次數剪枝
LeetCode 17: Letter Combinations of a Phone Number
·1 分鐘
LeetCode Medium Hash-Table String Backtracking
LeetCode 17 解題筆記:每一層處理一個數字,枚舉它對應的字母;空字串要回傳空 list
LeetCode 647: Palindromic Substrings
·1 分鐘
LeetCode Medium Two-Pointers String Dynamic-Programming
LeetCode 647 解題筆記:中心擴展,每擴展成功一次就是一個回文子字串
LeetCode 208: Implement Trie (Prefix Tree)
·1 分鐘
LeetCode Medium Hash-Table String Design Trie
LeetCode 208 解題筆記:每個節點是 children 字典加一個 is_end 標記;insert、search、startsWith 都是沿著字元往下走
LeetCode 424: Longest Repeating Character Replacement
·1 分鐘
LeetCode Medium Hash-Table String Sliding-Window
LeetCode 424 解題筆記:可變長度滑動視窗,視窗長度減掉最多的字元次數就是要替換的數量,超過 k 就縮左邊