每天一道leetcode27-移除元素 分类:双指针 中文链接:https://leetcode-cn.com/problems/remove-element/description /英文链接https://leetcode.com/problems/remove-element/description/
题目.png
思路
代码
class Solution { public int removeElement(int[] nums, int val) { int count = 0; int begin = 0; int end = nums.length - 1; while(begin <= end) { while(begin < nums.length && nums[begin] != val) begin++;
while(end >= 0 && nums[end] == val)
{
count++;
end--;
}
if(begin >= end || begin >= nums.length || end < 0)
break;
int temp = nums[begin];
nums[begin] = nums[end];
nums[end] = temp;
}
return nums.length - count;
}
}
代码讲解