Solve 27. Remove element (in place)

This commit is contained in:
Samuel Ortion 2025-03-01 18:22:58 +01:00
parent 66fbabec99
commit eabc14e3e5
Signed by: sortion
GPG Key ID: 9B02406F8C4FB765
2 changed files with 183 additions and 0 deletions

125
27.remove-element.org Normal file
View File

@ -0,0 +1,125 @@
#+title: 27 Remove Element
* Subject
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in 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:
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.
Example 1:
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
* Solution
#+name: solution
#+begin_src C
int removeElement(int* nums, int numsSize, int val) {
int right = 0;
int left = numsSize - 1;
int newSize = 0;
while (right <= left) {
if (nums[right] == val) {
nums[right] = nums[left];
left--;
} else {
newSize++;
right++;
}
}
return newSize;
}
#+end_src
#+name: driver
#+begin_src C :noweb yes :tangle "src/27.remove-element.c"
<<solution>>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
int comp(const void *element1, const void *element2) {
int f = *((int *) element1);
int s = *((int *) element2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
void test_1() {
int nums[] = {3,2,2,3};
int val = 3;
int k = removeElement(nums, 4, val);
int expectedNums[] = {2,2};
int expectedSize = 2;
assert(k == expectedSize);
qsort(nums, k, sizeof(int), comp);
for (int i=0; i < k; i++) {
assert(nums[i] == expectedNums[i]);
}
}
void test_2() {
int nums[] = {0,1,2,2,3,0,4,2};
int val = 2;
int k = removeElement(nums, 8, val);
int expectedNums[] = {0,0,1,3,4};
int expectedSize = 5;
assert(k == expectedSize);
qsort(nums, k, sizeof(int), comp);
for (int i=0; i < k; i++) {
assert(nums[i] == expectedNums[i]);
}
}
int main() {
test_1();
test_2();
}
#+end_src
#+RESULTS: driver

58
src/27.remove-element.c Normal file
View File

@ -0,0 +1,58 @@
int removeElement(int* nums, int numsSize, int val) {
int right = 0;
int left = numsSize - 1;
int newSize = 0;
while (right <= left) {
if (nums[right] == val) {
nums[right] = nums[left];
left--;
} else {
newSize++;
right++;
}
}
return newSize;
}
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
int comp(const void *element1, const void *element2) {
int f = *((int *) element1);
int s = *((int *) element2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
void test_1() {
int nums[] = {3,2,2,3};
int val = 3;
int k = removeElement(nums, 4, val);
int expectedNums[] = {2,2};
int expectedSize = 2;
assert(k == expectedSize);
qsort(nums, k, sizeof(int), comp);
for (int i=0; i < k; i++) {
assert(nums[i] == expectedNums[i]);
}
}
void test_2() {
int nums[] = {0,1,2,2,3,0,4,2};
int val = 2;
int k = removeElement(nums, 8, val);
int expectedNums[] = {0,0,1,3,4};
int expectedSize = 5;
assert(k == expectedSize);
qsort(nums, k, sizeof(int), comp);
for (int i=0; i < k; i++) {
assert(nums[i] == expectedNums[i]);
}
}
int main() {
test_1();
test_2();
}