26 lines
624 B
Java
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 ;
|
|
}
|
|
} |