Given an integer array
numsand an integerval, remove all occurrences ofvalinnumsnumswhich are not equal toval.Consider the number of elements in
numswhich are not equal tovalbek, to get accepted, you need to do the following things:
- Change the array
numssuch that the firstkelements ofnumscontain the elements which are not equal toval. The remaining elements ofnumsare not important as well as the size ofnums.- Return
k.Custom Judge:
The judge will test your solution with the following code:
1
2
3
4
5
6
7
8
9
10
11
12 >int[] nums = [...]; // Input array
>int val = ...; // Value to remove
>int[] expectedNums = [...]; // The expected answer with correct length.
// It is sorted with no values equaling val.
>int k = removeElement(nums, val); // Calls your implementation
>assert k == expectedNums.length;
>sort(nums, 0, k); // Sort the first k elements of nums
>for (int i = 0; i < actualLength; i++) {
assert nums[i] == expectedNums[i];
>}If all assertions pass, then your solution will be accepted.
1 | class Solution { |