|
| 1 | +<h2><a href="https://leetcode.com/problems/remove-element">27. Remove Element</a></h2><h3>Easy</h3><hr><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 order of the elements may be changed. Then return <em>the number of elements in </em><code>nums</code><em> which are not equal to </em><code>val</code>.</p> |
| 2 | + |
| 3 | +<p>Consider the number of elements in <code>nums</code> which are not equal to <code>val</code> be <code>k</code>, to get accepted, you need to do the following things:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Change the array <code>nums</code> such that the first <code>k</code> elements of <code>nums</code> contain the elements which are not equal to <code>val</code>. The remaining elements of <code>nums</code> are not important as well as the size of <code>nums</code>.</li> |
| 7 | + <li>Return <code>k</code>.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p><strong>Custom Judge:</strong></p> |
| 11 | + |
| 12 | +<p>The judge will test your solution with the following code:</p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +int[] nums = [...]; // Input array |
| 16 | +int val = ...; // Value to remove |
| 17 | +int[] expectedNums = [...]; // The expected answer with correct length. |
| 18 | + // It is sorted with no values equaling val. |
| 19 | + |
| 20 | +int k = removeElement(nums, val); // Calls your implementation |
| 21 | + |
| 22 | +assert k == expectedNums.length; |
| 23 | +sort(nums, 0, k); // Sort the first k elements of nums |
| 24 | +for (int i = 0; i < actualLength; i++) { |
| 25 | + assert nums[i] == expectedNums[i]; |
| 26 | +} |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p>If all assertions pass, then your solution will be <strong>accepted</strong>.</p> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong class="example">Example 1:</strong></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | +<strong>Input:</strong> nums = [3,2,2,3], val = 3 |
| 36 | +<strong>Output:</strong> 2, nums = [2,2,_,_] |
| 37 | +<strong>Explanation:</strong> Your function should return k = 2, with the first two elements of nums being 2. |
| 38 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 39 | +</pre> |
| 40 | + |
| 41 | +<p><strong class="example">Example 2:</strong></p> |
| 42 | + |
| 43 | +<pre> |
| 44 | +<strong>Input:</strong> nums = [0,1,2,2,3,0,4,2], val = 2 |
| 45 | +<strong>Output:</strong> 5, nums = [0,1,4,0,3,_,_,_] |
| 46 | +<strong>Explanation:</strong> Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. |
| 47 | +Note that the five elements can be returned in any order. |
| 48 | +It does not matter what you leave beyond the returned k (hence they are underscores). |
| 49 | +</pre> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>0 <= nums.length <= 100</code></li> |
| 56 | + <li><code>0 <= nums[i] <= 50</code></li> |
| 57 | + <li><code>0 <= val <= 100</code></li> |
| 58 | +</ul> |
0 commit comments