{"id":2949,"date":"2018-06-27T23:20:04","date_gmt":"2018-06-28T06:20:04","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2949"},"modified":"2018-06-27T23:36:16","modified_gmt":"2018-06-28T06:36:16","slug":"leetcode-228-summary-ranges","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-228-summary-ranges\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 228. Summary Ranges"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a sorted integer array without duplicates, return the summary of its ranges.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>  [0,1,2,4,5,7]\r\n<b>Output:<\/b> [\"0-&gt;2\",\"4-&gt;5\",\"7\"]\r\n<strong>Explanation: <\/strong>0,1,2 form a continuous range;\u00a04,5 form a continuous range.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false\"><b>Input:<\/b>  [0,2,3,4,6,8,9]\r\n<b>Output:<\/b> [\"0\",\"2-&gt;4\",\"6\",\"8-&gt;9\"]\r\n<strong>Explanation: <\/strong>2,3,4 form a continuous range;\u00a08,9 form a continuous range.<\/pre>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(k)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 3 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;string&gt; summaryRanges(vector&lt;int&gt;&amp; nums) {    \r\n    int s = 0;\r\n    vector&lt;string&gt; ans;\r\n    for (size_t i = 1; i &lt;= nums.size(); ++i) {\r\n      if (i == nums.size() || nums[i] != nums[i - 1] + 1) {\r\n        if (s == i - 1) \r\n          ans.push_back(to_string(nums[s]));\r\n        else\r\n          ans.push_back(to_string(nums[s]) + \"-&gt;\" + to_string(nums[i - 1]));\r\n        s = i;\r\n      }\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: [0,1,2,4,5,7] Output: [&#8220;0-&gt;2&#8243;,&#8221;4-&gt;5&#8243;,&#8221;7&#8221;] Explanation: 0,1,2 form a continuous&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,47],"tags":[20,177,4],"class_list":["post-2949","post","type-post","status-publish","format-standard","hentry","category-array","category-string","tag-array","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2949","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=2949"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2949\/revisions"}],"predecessor-version":[{"id":2952,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2949\/revisions\/2952"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}