Files
leetcode/src/cn/celess/leetcode/topinterview150/test27/Solution.java
2025-08-31 00:20:24 +08:00

26 lines
624 B
Java

package cn.celess.leetcode.topinterview150.test27;
class Solution {
public int removeElement(int[] nums, int val) {
int head = 0;
int tail = nums.length - 1;
while (tail >= 0 && head <= tail) {
if (nums[tail] == val) {
tail--;
continue;
}
if (nums[head] == val) {
int tmp = nums[head];
nums[head] = nums[tail];
nums[tail] = tmp;
head++;
tail--;
continue;
}
head++;
}
return head ;
}
}