{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "cc1b8932f88e4cc49bedde7f1da4b00a",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <stdbool.h>\n#include <string.h>\nstatic bool valid(char *ip, int len)\n{\n\tif (len > 1 && ip[0] == '0')\n\t{\n\t\treturn false;\n\t}\n\tif (len == 3)\n\t{\n\t\tint n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0');\n\t\tif (n > 255)\n\t\t{\n\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n#define WIDTH 4\nstatic void dfs(char *s, int start, char *stack, int num, char **results, int *count)\n{\n\tint i, j;\n\tif (num == 4)\n\t{\n\t\tif (s[start] == '\\0')\n\t\t{\n\t\t\tresults[*count] = malloc(3 * 4 + 3 + 1);\n\t\t\tchar *p = results[*count];\n\t\t\tfor (j = 0; j < num; j++)\n\t\t\t{\n\t\t\t\tchar *q = stack + j * WIDTH;\n\t\t\t\twhile ((*p++ = *q++) != '\\0')\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t\tif (j != 3)\n\t\t\t\t{\n\t\t\t\t\t*(p - 1) = '.';\n\t\t\t\t}\n\t\t\t}\n\t\t\t(*count)++;\n\t\t}\n\t}\n\telse\n\t{\n\t\tchar *p = stack + num * WIDTH;\n\t\tchar *q = p;\n\t\tfor (i = start; s[i] != '\\0' && i < start + 3; i++)\n\t\t{\n\t\t\t*q++ = s[i];\n\t\t\t*q = '\\0';\n\t\t\tif (!valid(p, q - p))\n\t\t\t{\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tdfs(s, i + 1, stack, num + 1, results, count);\n\t\t\tif (num + 1 < 4)\n\t\t\t{\n\t\t\t\tmemset(stack + (num + 1) * WIDTH, 0, WIDTH);\n\t\t\t}\n\t\t}\n\t}\n}\nstatic char **restoreIpAddresses(char *s, int *returnSize)\n{\n\tint count = 0;\n\tchar **results = malloc(100 * sizeof(char *));\n\tchar addr[16] = {'\\0'};\n\tdfs(s, 0, addr, 0, results, &count);\n\t*returnSize = count;\n\treturn results;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test num\\n\");\n\t\texit(-1);\n\t}\n\tint i, count = 0;\n\tchar **list = restoreIpAddresses(argv[1], &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%s\\n\", list[i]);\n\t}\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-bc7ff3f59cfd44729a2396f96ae5506c",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470923"
}