|
| 1 | +<h2> Remove Element</h2><hr><div><p>Given an integer array <code>nums</code> and an integer <code>val</code>, remove all occurrences of <code>val</code> in <code>nums</code> <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a>. The relative order of the elements may be changed.</p> |
| 2 | + |
| 3 | +<p>Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the <strong>first part</strong> of the array <code>nums</code>. More formally, if there are <code>k</code> elements after removing the duplicates, then the first <code>k</code> elements of <code>nums</code> should hold the final result. It does not matter what you leave beyond the first <code>k</code> elements.</p> |
| 4 | + |
| 5 | +<p>Return <code>k</code><em> after placing the final result in the first </em><code>k</code><em> slots of </em><code>nums</code>.</p> |
| 6 | + |
| 7 | +<p>Do <strong>not</strong> allocate extra space for another array. You must do this by <strong>modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a></strong> with O(1) extra memory.</p> |
| 8 | + |
| 9 | +<p><strong>Custom Judge:</strong></p> |
| 10 | + |
| 11 | +<p>The judge will test your solution with the following code:</p> |
| 12 | + |
| 13 | +<pre>int[] nums = [...]; // Input array |
| 14 | +int val = ...; // Value to remove |
| 15 | +int[] expectedNums = [...]; // The expected answer with correct length. |
| 16 | + // It is sorted with no values equaling val. |
| 17 | + |
| 18 | +int k = removeElement(nums, val); // Calls your implementation |
| 19 | + |
| 20 | +assert k == expectedNums.length; |
| 21 | +sort(nums, 0, k); // Sort the first k elements of nums |
| 22 | +for (int i = 0; i < actualLength; i++) { |
| 23 | + assert nums[i] == expectedNums[i]; |
| 24 | +} |
| 25 | +</pre> |
| 26 | + |
| 27 | +<p>If all assertions pass, then your solution will be <strong>accepted</strong>.</p> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Example 1:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>Input:</strong> nums = [3,2,2,3], val = 3 |
| 33 | +<strong>Output:</strong> 2, nums = [2,2,_,_] |
| 34 | +<strong>Explanation:</strong> Your function should return k = 2, with the first two elements of nums being 2. |
| 35 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 36 | +</pre> |
| 37 | + |
| 38 | +<p><strong>Example 2:</strong></p> |
| 39 | + |
| 40 | +<pre><strong>Input:</strong> nums = [0,1,2,2,3,0,4,2], val = 2 |
| 41 | +<strong>Output:</strong> 5, nums = [0,1,4,0,3,_,_,_] |
| 42 | +<strong>Explanation:</strong> Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. |
| 43 | +Note that the five elements can be returned in any order. |
| 44 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p> </p> |
| 48 | +<p><strong>Constraints:</strong></p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li><code>0 <= nums.length <= 100</code></li> |
| 52 | + <li><code>0 <= nums[i] <= 50</code></li> |
| 53 | + <li><code>0 <= val <= 100</code></li> |
| 54 | +</ul> |
| 55 | +</div> |
0 commit comments