{"id":5653,"date":"2019-09-30T08:52:33","date_gmt":"2019-09-30T15:52:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5653"},"modified":"2019-09-30T08:53:40","modified_gmt":"2019-09-30T15:53:40","slug":"leetcode-93-restore-ip-addresses","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-93-restore-ip-addresses\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 93. Restore IP Addresses"},"content":{"rendered":"\n<p>Given a string containing only digits, restore it by returning all possible valid IP address combinations.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \"25525511135\"\n<strong>Output:<\/strong> <code>[\"255.255.11.135\", \"255.255.111.35\"]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>The range of valid numbers is [0, 255]<\/p>\n\n\n\n<p>Time complexity: O(3^4)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<string> restoreIpAddresses(string s) {\n    vector<string> ans;\n    string ip;\n    dfs(0, s, ip, ans);\n    return ans;\n  }\n    \nprivate: \n  void dfs(int d, string s, string ip, vector<string> &ans) {\n    int l = s.length();\n    if (d == 4) {\n      if (l == 0) ans.push_back(ip);\n      return;\n    }\n    \n    for (int i = 1; i <= min(3, s[0] == '0' ? 1 : l); i++) {\n      string ss = s.substr(0, i);      \n      if (i == 3 &#038;&#038; stoi(ss) > 255) return;      \n      dfs(d + 1, s.substr(i), ip + (d == 0 ? \"\" : \".\") + ss , ans);\n    }\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string containing only digits, restore it by returning all possible valid IP address combinations. Example: Input: &#8220;25525511135&#8221; Output: [&#8220;255.255.11.135&#8221;, &#8220;255.255.111.35&#8221;] Solution: DFS The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42],"class_list":["post-5653","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5653"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5653\/revisions"}],"predecessor-version":[{"id":5656,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5653\/revisions\/5656"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}