{"id":5669,"date":"2019-09-30T23:30:11","date_gmt":"2019-10-01T06:30:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5669"},"modified":"2019-09-30T23:30:54","modified_gmt":"2019-10-01T06:30:54","slug":"leetcode-80-remove-duplicates-from-sorted-array-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-80-remove-duplicates-from-sorted-array-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 80. Remove Duplicates from Sorted Array II"},"content":{"rendered":"\n<p>Given a sorted array&nbsp;<em>nums<\/em>, remove the duplicates&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>in-place<\/strong><\/a>&nbsp;such that duplicates appeared at most&nbsp;<em>twice<\/em>&nbsp;and return the new length.<\/p>\n\n\n\n<p>Do not allocate extra space for another array, you must do this by&nbsp;<strong>modifying the input array&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noreferrer noopener\">in-place<\/a><\/strong>&nbsp;with O(1) extra memory.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Given <em>nums<\/em> = <strong>[1,1,1,2,2,3]<\/strong>,\n\nYour function should return length = <strong><code>5<\/code><\/strong>, with the first five elements of <em><code>nums<\/code><\/em> being <strong><code>1, 1, 2, 2<\/code><\/strong> and <strong>3<\/strong> respectively.\n\nIt doesn't matter what you leave beyond the returned length.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">Given <em>nums<\/em> = <strong>[0,0,1,1,1,1,2,3,3]<\/strong>,\n\nYour function should return length = <strong><code>7<\/code><\/strong>, with the first seven elements of <em><code>nums<\/code><\/em> being modified to&nbsp;<strong><code>0<\/code><\/strong>, <strong>0<\/strong>, <strong>1<\/strong>, <strong>1<\/strong>, <strong>2<\/strong>, <strong>3<\/strong> and&nbsp;<strong>3<\/strong> respectively.\n\nIt doesn't matter what values are set beyond&nbsp;the returned length.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<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  int removeDuplicates(vector<int>& nums) {\n    const int n = nums.size();\n    if (n <= 2) return n;\n\n    int index = 1;\n    int len = 1;\n    int last = nums[0];\n\n    while (index < n) {\n      int count = 1;\n      while (index < n &#038;&#038; nums[index] == last) {\n        ++count;\n        ++index;\n      }\n\n      if (count >= 2) nums[len++] = last;        \n\n      if (index < n) {\n        last = nums[index++];\n        nums[len++] = last;\n      }\n    }\n\n    return len;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-82-remove-duplicates-from-sorted-list-ii\/\">https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-82-remove-duplicates-from-sorted-list-ii\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given a sorted array&nbsp;nums, remove the duplicates&nbsp;in-place&nbsp;such that duplicates appeared at most&nbsp;twice&nbsp;and return the new length. Do not allocate extra space for another array, you&#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,333,177],"class_list":["post-5669","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-duplicate","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5669","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=5669"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5669\/revisions"}],"predecessor-version":[{"id":5671,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5669\/revisions\/5671"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}