{"id":4135,"date":"2018-10-02T12:32:20","date_gmt":"2018-10-02T19:32:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4135"},"modified":"2018-10-02T12:32:37","modified_gmt":"2018-10-02T19:32:37","slug":"leetcode-27-remove-element","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-27-remove-element\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 27. Remove Element"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<em>nums<\/em>\u00a0and a value\u00a0<em>val<\/em>, remove all instances of that value\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/In-place_algorithm\" target=\"_blank\" rel=\"noopener\"><strong>in-place<\/strong><\/a>\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>The order of elements can be changed. It doesn&#8217;t matter what you leave beyond the new length.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\">Given <em>nums<\/em> = <strong>[3,2,2,3]<\/strong>, <em>val<\/em> = <strong>3<\/strong>,\r\n\r\nYour function should return length = <strong>2<\/strong>, with the first two elements of <em>nums<\/em> being <strong>2<\/strong>.\r\n\r\nIt doesn't matter what you leave beyond the returned length.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\">Given <em>nums<\/em> = <strong>[0,1,2,2,3,0,4,2]<\/strong>, <em>val<\/em> = <strong>2<\/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> containing\u00a0<strong><code>0<\/code><\/strong>, <strong><code>1<\/code><\/strong>, <strong><code>3<\/code><\/strong>, <strong><code>0<\/code><\/strong>, and\u00a0<strong>4<\/strong>. Note that the order of those five elements can be arbitrary. 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 = removeElement(nums, val);\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><strong>Solution:<\/strong><\/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 removeElement(vector&lt;int&gt;&amp; nums, int val) {\r\n    int len = 0;\r\n    for (int num : nums)\r\n      if (num != val) nums[len++] = num;\r\n    return len;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">class Solution:\r\n  def removeElement(self, nums, val):\r\n    l = 0\r\n    for num in nums:\r\n      if num != val:\r\n        nums[l], l = num, l + 1        \r\n    return l<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-26-remove-duplicates-from-sorted-array\/\">\u82b1\u82b1\u9171 LeetCode 26. Remove Duplicates from Sorted Array<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0nums\u00a0and a value\u00a0val, remove all instances of that value\u00a0in-place\u00a0and 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":[222,273,252],"class_list":["post-4135","post","type-post","status-publish","format-standard","hentry","category-array","tag-easy","tag-in-place","tag-remove","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4135","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=4135"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions"}],"predecessor-version":[{"id":4137,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4135\/revisions\/4137"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}