feat: 27_remove_element
This commit is contained in:
parent
047db7b742
commit
02a7e000a4
21
27_remove_element/main.cpp
Normal file
21
27_remove_element/main.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val) {
|
||||
if (nums.size() == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int k = 0, lastNum = nums[0];
|
||||
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
if (nums[i] != val) {
|
||||
nums[k++] = nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
return k;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user