{"id":643,"date":"2020-09-26T11:06:41","date_gmt":"2020-09-26T03:06:41","guid":{"rendered":"http:\/\/39.96.58.60\/?p=643"},"modified":"2022-10-18T16:38:24","modified_gmt":"2022-10-18T08:38:24","slug":"leetcode-93-%e5%a4%8d%e5%8e%9fip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=643","title":{"rendered":"Leetcode 93. \u590d\u539fIP\u5730\u5740"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff0c\u590d\u539f\u5b83\u5e76\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684 IP \u5730\u5740\u683c\u5f0f\u3002<\/p>\n<p><strong>\u6709\u6548\u7684 IP \u5730\u5740<\/strong> \u6b63\u597d\u7531\u56db\u4e2a\u6574\u6570\uff08\u6bcf\u4e2a\u6574\u6570\u4f4d\u4e8e 0 \u5230 255 \u4e4b\u95f4\u7ec4\u6210\uff0c\u4e14\u4e0d\u80fd\u542b\u6709\u524d\u5bfc <code>0<\/code>\uff09\uff0c\u6574\u6570\u4e4b\u95f4\u7528 <code>'.' <\/code>\u5206\u9694\u3002<\/p>\n<p>\u4f8b\u5982\uff1a&#8221;0.1.2.201&#8243; \u548c &#8220;192.168.1.1&#8221; \u662f <strong>\u6709\u6548\u7684<\/strong> IP \u5730\u5740\uff0c\u4f46\u662f &#8220;0.011.255.245&#8221;\u3001&#8221;192.168.1.312&#8243; \u548c &#8220;192.168@1.1&#8221; \u662f <strong>\u65e0\u6548\u7684<\/strong> IP \u5730\u5740\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"25525511135\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\"255.255.11.135\",\"255.255.111.35\"]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"0000\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\"0.0.0.0\"]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"1111\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\"1.1.1.1\"]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"010010\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\"0.10.0.10\",\"0.100.1.0\"]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"101023\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>[\"1.0.10.23\",\"1.0.102.3\",\"10.1.0.23\",\"10.10.2.3\",\"101.0.2.3\"]\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>0 &lt;= s.length &lt;= 3000<\/code><\/li>\n<li><code>s<\/code> \u4ec5\u7531\u6570\u5b57\u7ec4\u6210<\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001 \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8689.64% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8668.81% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def restoreIpAddresses(self, s: str) -&gt; List[str]:\n        result = []\n        def backtrack(s, current:list): #   \u5f53\u524d\u5b57\u7b26\u4e32\uff0c\u5f53\u524dip\u5217\u8868\n            # print(s, current)\n            if s =='' and len(current) == 4:    # \u5982\u679c\u5b57\u7b26\u4e32\u5168\u4f7f\u7528\uff0c\u4e14\u5f53\u524dip\u957f\u5ea6\u521a\u597d4\u4f4d\u3002\u5219\u5b8c\u5168\u5339\u914d\n                result.append(current[:])       # \u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n                return\n\n            if ((4-len(current)) * 3 &lt; len(s)): # \u5982\u679c \u5269\u4f59\u5b57\u7b26\u4e32\u6570\u91cf\uff0c\u5927\u4e8e\u5f53\u524d\u7f3a\u5931\u7684ip*3 \u5219\uff0c\u5b57\u7b26\u4e32\u5197\u4f59\uff0c\u8df3\u51fa\n                return\n\n            for i in range(min(3, len(s))):     # ip\u6bcf\u4e00\u4f4d\u6700\u59273\u4e2a\u5b57\u7b26\n                if (s[0] == '0' and i &lt; 1) or (s[0] != '0' and 0&lt; (int(s[:i+1]) &lt; 256)):    # \u5f53\u524d\u5b57\u7b26\uff0c\u4ee50\u5f00\u5934\u4f7f\uff0c\u53ea\u80fd\u5339\u914d0\uff1b \u6216\u4e0d\u4ee50\u5f00\u5934\u4e14\u57280\uff5e255\u4e4b\u95f4\n                    current.append(s[:i+1])\n                    now_s = s[i+1:]\n                    backtrack(now_s, current)   # \u4ece\u5269\u4f59\u5b57\u7b26\u4e32\u4e2d\u5339\u914d\u4e0b\u4e00\u4f4d\n                    current.pop()   # \u56de\u6eaf\n\n        backtrack(s, [])\n        result = [ '.'.join(i) for i in result] # \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u7684\u5b57\u7b26\u4e32\uff0c\u590d\u539f\u5b83\u5e76\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u7684 IP \u5730\u5740\u683c\u5f0f\u3002 \u6709\u6548\u7684 IP \u5730\u5740 \u6b63\u597d\u7531\u56db\u4e2a\u6574\u6570\uff08\u6bcf\u4e2a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=643"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":644,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions\/644"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}