{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "9f9ae60bd464481d9f430ae229754d44",
  "keywords": "字符串,回溯",
  "title": "复原 IP 地址",
  "desc": [
    {
      "content": "\n<p>给定一个只包含数字的字符串，用以表示一个 IP 地址，返回所有可能从 <code>s</code> 获得的 <strong>有效 IP 地址 </strong>。你可以按任何顺序返回答案。</p><p><strong>有效 IP 地址</strong> 正好由四个整数（每个整数位于 0 到 255 之间组成，且不能含有前导 <code>0</code>），整数之间用 <code>'.'</code> 分隔。</p><p>例如：\"0.1.2.201\" 和 \"192.168.1.1\" 是 <strong>有效</strong> IP 地址，但是 \"0.011.255.245\"、\"192.168.1.312\" 和 \"192.168@1.1\" 是 <strong>无效</strong> IP 地址。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = \"25525511135\"<strong><br />输出：</strong>[\"255.255.11.135\",\"255.255.111.35\"]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = \"0000\"<strong><br />输出：</strong>[\"0.0.0.0\"]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s = \"1111\"<strong><br />输出：</strong>[\"1.1.1.1\"]</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>s = \"010010\"<strong><br />输出：</strong>[\"0.10.0.10\",\"0.100.1.0\"]</pre><p><strong>示例 5：</strong></p><pre><strong>输入：</strong>s = \"101023\"<strong><br />输出：</strong>[\"1.0.10.23\",\"1.0.102.3\",\"10.1.0.23\",\"10.10.2.3\",\"101.0.2.3\"]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li><code>0 <= s.length <= 3000</code></li>\t<li><code>s</code> 仅由数字组成</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution(object):\n\tdef restoreIpAddresses(self, s):\n\t\tls = len(s)\n\t\tif ls == 0 or ls > 12:\n\t\t\treturn []\n\t\tres = []\n\t\tfor i in range(1, 4):\n\t\t\tfor j in range(1, 4):\n\t\t\t\tfor k in range(1, 4):\n\t\t\t\t\tm = ls - i - j - k\n\t\t\t\t\tif m > 0 and m <= 3:\n\t\t\t\t\t\tadd1 = s[0:i]\n\t\t\t\t\t\tadd2 = s[i:i + j]\n\t\t\t\t\t\tadd3 = s[i + j:i + j + k]\n\t\t\t\t\t\tadd4 = s[i + j + k:]\n\t\t\t\t\t\tif self.isValid(add1) and self.isValid(add2) and \\\n\t\t\t\t\t\t\t\t\t\tself.isValid(add3) and self.isValid(add4):\n\t\t\t\t\t\t\tres.append(add1 + '.' + add2 + '.' + add3 + '.' + add4)\n\t\treturn res\n\tdef isValid(self, add):\n\t\tif len(add) == 1:\n\t\t\treturn True\n\t\tif add[0] == '0':\n\t\t\treturn False\n\t\tif int(add) <= 255:\n\t\t\treturn True\n\t\treturn False\nif __name__ == '__main__':\n\ts = Solution()\n\tprint (s.restoreIpAddresses('25525511135'))",
    "language": "python"
  },
  "node_id": "dailycode-f5007b90ce784459a128ea75d2f74143",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470923"
}