- Published on
删除数组中的指定元素-----复习
- Authors
- Name
- DP Piggy
- @xiaozhudxiaozhu
java
public int removeElement(int[] nums, int val) {
int slow = 0;
int fast = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
go
func removeElement(nums []int, val int) int {
slow := 0
fast := 0
for fast < len(nums) {
if nums[fast] != val {
nums[slow] = nums[fast]
slow++
}
fast++
}
return slow
}
c++
int removeElement(vector<int>& nums, int val) {
int slow = 0;
int fast = 0;
while (fast < nums.size()) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}