{ "question_id": 93, "question_title": "复原 IP 地址", "difficulty": "中等", "question_content": "

给定一个只包含数字的字符串,用以表示一个 IP 地址,返回所有可能从 s 获得的 有效 IP 地址 。你可以按任何顺序返回答案。

有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。

例如:\"0.1.2.201\" 和 \"192.168.1.1\" 是 有效 IP 地址,但是 \"0.011.255.245\"、\"192.168.1.312\" 和 \"192.168@1.1\" 是 无效 IP 地址。

 

示例 1:

输入:s = \"25525511135\"
输出:
[\"255.255.11.135\",\"255.255.111.35\"]

示例 2:

输入:s = \"0000\"
输出:
[\"0.0.0.0\"]

示例 3:

输入:s = \"1111\"
输出:
[\"1.1.1.1\"]

示例 4:

输入:s = \"010010\"
输出:
[\"0.10.0.10\",\"0.100.1.0\"]

示例 5:

输入:s = \"101023\"
输出:
[\"1.0.10.23\",\"1.0.102.3\",\"10.1.0.23\",\"10.10.2.3\",\"101.0.2.3\"]

 

提示:

", "topic_link": "https://bbs.csdn.net/topics/600470923", "cpp": "#include \n#include \n#include \n#include \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}", "java": "class Solution {\n\tprivate List res = new ArrayList<>();\n\n\tpublic List restoreIpAddresses(String s) {\n\t\tif (s.length() < 4)\n\t\t\treturn res;\n\t\tbacktrack(s, 0, new StringBuilder(), 0);\n\t\treturn res;\n\t}\n\n\tprivate void backtrack(String s, int start, StringBuilder sb, int pointNumOfSb) {\n\t\tif (pointNumOfSb > 4)\n\t\t\treturn;\n\t\tif (start == s.length() && pointNumOfSb == 4) {\n\t\t\tres.add(sb.toString().substring(1));\n\t\t\treturn;\n\t\t}\n\t\tfor (int i = start; i < s.length() && i - start < 3; i++) {\n\t\t\tString x = s.substring(start, i + 1);\n\t\t\tif (x.charAt(0) == '0' && x.length() > 1)\n\t\t\t\treturn;\n\t\t\tif (Integer.parseInt(x) <= 255) {\n\t\t\t\tsb.append(\".\" + x);\n\t\t\t\tbacktrack(s, i + 1, sb, pointNumOfSb + 1);\n\t\t\t\tsb.delete(sb.lastIndexOf(\".\"), sb.length());\n\t\t\t}\n\t\t}\n\t}\n}", "js": "", "python": "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'))", "status": 1, "keywords": "字符串,回溯", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://github.com/zhangyu345293721/leetcode" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/92/92_java.ipynb?type=file" }, "notebook_enable": 1 }