{"id":2484,"date":"2018-04-14T20:15:39","date_gmt":"2018-04-15T03:15:39","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2484"},"modified":"2018-04-15T01:04:16","modified_gmt":"2018-04-15T08:04:16","slug":"leetcode-816-ambiguous-coordinates","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-816-ambiguous-coordinates\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 816. Ambiguous Coordinates"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u628a\u4e00\u4e32\u6570\u5b57\u5206\u6210\u4e24\u6bb5\uff0c\u8f93\u51fa\u6240\u6709\u5408\u6cd5\u7684\u5206\u6cd5\uff08\u53ef\u4ee5\u52a0\u5c0f\u6570\u70b9\uff09\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/ambiguous-coordinates\/description\/\">https:\/\/leetcode.com\/problems\/ambiguous-coordinates\/description\/<\/a><\/p>\n<p>We had some 2-dimensional coordinates, like\u00a0<code>\"(1, 3)\"<\/code>\u00a0or\u00a0<code>\"(2, 0.5)\"<\/code>.\u00a0 Then, we removed\u00a0all commas, decimal points, and spaces, and ended up with the string\u00a0<code>S<\/code>.\u00a0 Return a list of strings representing\u00a0all possibilities for what our original coordinates could have been.<\/p>\n<p>Our original representation never had extraneous zeroes, so we never started with numbers like &#8220;00&#8221;, &#8220;0.0&#8221;, &#8220;0.00&#8221;, &#8220;1.0&#8221;, &#8220;001&#8221;, &#8220;00.01&#8221;, or any other number that can be represented with\u00a0less digits.\u00a0 Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like &#8220;.1&#8221;.<\/p>\n<p>The final answer list can be returned in any order.\u00a0 Also note that all coordinates in the final answer\u00a0have exactly one space between them (occurring after the comma.)<\/p>\n<pre class=\"crayon:false\"><strong>Example 1:<\/strong>\r\n<strong>Input:<\/strong> \"(123)\"\r\n<strong>Output:<\/strong> [\"(1, 23)\", \"(12, 3)\", \"(1.2, 3)\", \"(1, 2.3)\"]\r\n<\/pre>\n<pre class=\"crayon:false\"><strong>Example 2:<\/strong>\r\n<strong>Input:<\/strong> \"(00011)\"\r\n<strong>Output:<\/strong> \u00a0[\"(0.001, 1)\", \"(0, 0.011)\"]\r\n<strong>Explanation:<\/strong> \r\n0.0, 00, 0001 or 00.01 are not allowed.\r\n<\/pre>\n<pre class=\"crayon:false\"><strong>Example 3:<\/strong>\r\n<strong>Input:<\/strong> \"(0123)\"\r\n<strong>Output:<\/strong> [\"(0, 123)\", \"(0, 12.3)\", \"(0, 1.23)\", \"(0.1, 23)\", \"(0.1, 2.3)\", \"(0.12, 3)\"]\r\n<\/pre>\n<pre class=\"crayon:false\"><strong>Example 4:<\/strong>\r\n<strong>Input:<\/strong> \"(100)\"\r\n<strong>Output:<\/strong> [(10, 0)]\r\n<strong>Explanation:<\/strong> \r\n1.0 is not allowed.\r\n<\/pre>\n<h1><strong>Solution: Brute Force<\/strong><\/h1>\n<p>Time complexity: O(n^3)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; ambiguousCoordinates(string S) {\r\n    vector&lt;string&gt; ans;\r\n    int n = S.length();\r\n    for (int l1 = 1; l1 &lt;= n - 3; ++l1) {      \r\n      int l2 = n - l1 - 2;\r\n      string s1 = S.substr(1, l1);\r\n      string s2 = S.substr(l1 + 1, l2);      \r\n      if (valid(s1) &amp;&amp; valid(s2))\r\n        ans.push_back(\"(\" + s1 + \", \" + s2 + \")\");\r\n      \r\n      for (int i = 0; i &lt; l1; ++i) {\r\n        string ts1 = s1;\r\n        if (i &gt; 0) ts1.insert(i, \".\");        \r\n        if (!valid(ts1)) continue;\r\n        for (int j = 0; j &lt; l2; ++j) {\r\n          if (i == 0 &amp;&amp; j == 0) continue;\r\n          string ts2 = s2;\r\n          if (j &gt; 0) ts2.insert(j, \".\");          \r\n          if (!valid(ts2)) continue;          \r\n          ans.push_back(\"(\" + ts1 + \", \" + ts2 + \")\");\r\n        }\r\n      }\r\n    }\r\n    \r\n    return ans;\r\n  }\r\nprivate:    \r\n  bool valid(const string&amp; s) {\r\n    bool p = false; \/\/ has \".\"\r\n    bool d = false; \/\/ has digits in front of \".\"\r\n    int zeros = 0;  \/\/ leading zeros\r\n    int i = 0;\r\n    while (s[zeros] == '0' &amp;&amp; zeros &lt; s.length()) ++zeros;\r\n    for (int i = zeros; i &lt; s.length(); ++i) {\r\n      if (!p &amp;&amp; isdigit(s[i])) d = true;\r\n      if (s[i] == '.') p = true;\r\n    }\r\n    if (zeros == 1 &amp;&amp; s != \"0\" &amp;&amp; !p || zeros &gt; 1) return false;\r\n    if (p &amp;&amp; (s.back() == '0' || s.back() == '.' || zeros &gt; 0 &amp;&amp; d)) return false;    \r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u628a\u4e00\u4e32\u6570\u5b57\u5206\u6210\u4e24\u6bb5\uff0c\u8f93\u51fa\u6240\u6709\u5408\u6cd5\u7684\u5206\u6cd5\uff08\u53ef\u4ee5\u52a0\u5c0f\u6570\u70b9\uff09\u3002 https:\/\/leetcode.com\/problems\/ambiguous-coordinates\/description\/ We had some 2-dimensional coordinates, like\u00a0&#8220;(1, 3)&#8221;\u00a0or\u00a0&#8220;(2, 0.5)&#8221;.\u00a0 Then, we removed\u00a0all commas, decimal points, and spaces, and ended up with the string\u00a0S.\u00a0&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[297,65,4,286],"class_list":["post-2484","post","type-post","status-publish","format-standard","hentry","category-string","tag-format","tag-number","tag-string","tag-valid","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2484","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=2484"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2484\/revisions"}],"predecessor-version":[{"id":2498,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2484\/revisions\/2498"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}