Given an integer array
nums
and an integerval
, remove all occurrences ofval
innums
nums
which are not equal toval
.Consider the number of elements in
nums
which are not equal toval
bek
, to get accepted, you need to do the following things:
- Change the array
nums
such that the firstk
elements ofnums
contain the elements which are not equal toval
. The remaining elements ofnums
are 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 { |