|
| 1 | +# Ex 002: Remove Element |
| 2 | + |
| 3 | +Practice Problem: |
| 4 | + |
| 5 | +Given an integer array `nums` and an integer `val`, remove all occurrences of |
| 6 | +`val` in `nums` in-place. The order of the elements may be changed. Then return |
| 7 | +the number of elements in nums which are not equal to `val`. |
| 8 | + |
| 9 | +Consider the number of elements in nums which are not equal to `val` be `k`, to |
| 10 | +get accepted, you need to do the following things: |
| 11 | +- Change the array `nums` such that the first `k` elements of `nums` contain the |
| 12 | + elements which are not equal to `val`. The remaining elements of `nums` are not |
| 13 | + important as well as the size of `nums`. |
| 14 | +- Return k. |
| 15 | + |
| 16 | +Custom Judge: |
| 17 | +The judge will test your solution with the following code: |
| 18 | +``` |
| 19 | +int[] nums = [...]; // Input array |
| 20 | +int val = ...; // Value to remove |
| 21 | +int[] expectedNums = [...]; // The expected answer with correct length. |
| 22 | + // It is sorted with no values equaling val. |
| 23 | +
|
| 24 | +int k = removeElement(nums, val); // Calls your implementation |
| 25 | +
|
| 26 | +assert k == expectedNums.length; |
| 27 | +sort(nums, 0, k); // Sort the first k elements of nums |
| 28 | +for (int i = 0; i < actualLength; i++) { |
| 29 | + assert nums[i] == expectedNums[i]; |
| 30 | +} |
| 31 | +``` |
| 32 | +If all assertions pass, then your solution will be accepted. |
| 33 | + |
| 34 | +Source Info: |
| 35 | +- [Leetcode 27: Remove Element](https://leetcode.com/problems/remove-element/description/) |
| 36 | +- Topics: \[Array, Two Pointers\] |
| 37 | + |
| 38 | +A: |
| 39 | +Impl in 002_remove_element_test.go |
| 40 | + |
0 commit comments