{"id":4130,"date":"2018-10-02T09:33:53","date_gmt":"2018-10-02T16:33:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4130"},"modified":"2018-10-02T13:17:05","modified_gmt":"2018-10-02T20:17:05","slug":"leetcode-26-remove-duplicates-from-sorted-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-26-remove-duplicates-from-sorted-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 26. Remove Duplicates from Sorted Array"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a sorted array\u00a0<em>nums<\/em>, remove the duplicates\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\"><strong>in-place<\/strong><\/a>\u00a0such that each element appear only\u00a0<em>once<\/em>\u00a0and return the new length.<\/p>\n<p>Do not allocate extra space for another array, you must do this by\u00a0<strong>modifying the input array\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\">in-place<\/a><\/strong>\u00a0with O(1) extra memory.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\">Given <em>nums<\/em> = <strong>[1,1,2]<\/strong>,\r\n\r\nYour function should return length = <strong><code>2<\/code><\/strong>, with the first two elements of <em><code>nums<\/code><\/em> being <strong><code>1<\/code><\/strong> and <strong><code>2<\/code><\/strong> respectively. It doesn't matter what you leave beyond the returned length.<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\">Given <em>nums<\/em> = <strong>[0,0,1,1,1,2,2,3,3,4]<\/strong>,\r\n\r\nYour function should return length = <strong><code>5<\/code><\/strong>, with the first five elements of <em><code>nums<\/code><\/em> being modified to\u00a0<strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>2<\/code><\/strong>, <strong><code>3<\/code><\/strong>, and\u00a0<strong><code>4<\/code><\/strong> respectively. It doesn't matter what values are set beyond\u00a0the returned length.<\/pre>\n<p><strong>Clarification:<\/strong><\/p>\n<p>Confused why the returned value is an integer but your answer is an array?<\/p>\n<p>Note that the input array is passed in by\u00a0<strong>reference<\/strong>, which means modification to the input array will be known to the caller as well.<\/p>\n<p>Internally you can think of this:<\/p>\n<pre class=\"crayon:false\">\/\/ <strong>nums<\/strong> is passed in by reference. (i.e., without making a copy)\r\nint len = removeDuplicates(nums);\r\n\r\n\/\/ any modification to <strong>nums<\/strong> in your function would be known by the caller.\r\n\/\/ using the length returned by your function, it prints the first <strong>len<\/strong> elements.\r\nfor (int i = 0; i &lt; len; i++) {\r\n\u00a0 \u00a0 print(nums[i]);\r\n}<\/pre>\n<h1>Solution:<\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true  \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  int removeDuplicates(vector&lt;int&gt;&amp; nums) {\r\n    const int n = nums.size();\r\n    if (n &lt;= 1) return n;        \r\n    int ans = 0;\r\n    int i = 0;\r\n    while (i &lt; n) {\r\n      nums[ans++] = nums[i];\r\n      int j = i + 1;\r\n      while (j &lt; n &amp;&amp; nums[j] == nums[j - 1]) ++j;\r\n      i = j;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a sorted array\u00a0nums, remove the duplicates\u00a0in-place\u00a0such that each element appear only\u00a0once\u00a0and return the new length. Do not allocate extra space for another array,&#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],"tags":[20,21,222,252,23],"class_list":["post-4130","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-deduplication","tag-easy","tag-remove","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4130","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=4130"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4130\/revisions"}],"predecessor-version":[{"id":4145,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4130\/revisions\/4145"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}