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