Time: 0 ms (100.00%), Space: 41.5 MB (21.47%) - LeetHub

This commit is contained in:
Deven
2023-05-21 22:17:14 -04:00
parent 295d86bad2
commit ea99a8108e
+23
View File
@@ -0,0 +1,23 @@
class Solution {
public int removeElement(int[] nums, int val) {
if (nums.length == 0) {
return 0;
}
int backIndex = nums.length - 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;
}
}