mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 0 ms (100.00%), Space: 5.8 MB (93.19%) - LeetHub
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user