[CS Academy] Many ZerosRead more
[CS Academy] Milk and BreadRead more
[Codeforces] Round 533 (Div. 2) D. Kilani and the GameRead more
[Codeforces] Round 538 (Div. 2) D. Flood FillRead more
[Codeforces] Educational Round 60 (Rated for Div. 2) C. Magic ShipRead more
[Codeforces] Global Round 2 E. Pavel and TrianglesRead more
[Codeforces] Round 543 (Div. 1, based on Technocup 2019 Final Round) A. Diana and LianaRead more
[Codeforces] Round 544 (Div. 3) F2. Spanning Tree with One Fixed DegreeRead more
[Codeforces] Round 550 (Div. 3) E. Median StringRead more
[LeetCode] Remove Element

27. Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.
  • 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.

Read more