Time: 0 ms (100.00%), Space: 5.8 MB (93.19%) - LeetHub

This commit is contained in:
Deven
2023-05-21 22:17:32 -04:00
parent 427e78998d
commit 6ce7f40724
+21
View File
@@ -0,0 +1,21 @@
int removeElement(int* nums, int numsSize, int val){
if (numsSize == 0) {
return 0;
}
int backIndex = numsSize - 1;
for (int i = 0; i < backIndex; ++i) {
if (nums[i] == val) {
while (nums[backIndex] == val) {
backIndex--;
if (backIndex == i) {
return nums[backIndex] == val ? backIndex : backIndex + 1;
}
}
nums[i] = nums[backIndex];
nums[backIndex] = val;
backIndex--;
}
}
return nums[backIndex] == val ? backIndex : backIndex + 1;
}