*************
c++
topic:27. 移除元素 - 力扣(LeetCode)
*************
Last case I moved all 0 in the end, and this one is to delete the zero:
inspect the last projest's code:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n = nums.size();
int j = 0;
for(int i = 0; i < n; i++){
if (nums[i] != val){
nums[j] = nums[i];
j++;
}
}
for(int i = j; i < n; i++){
nums[i] = 1;
}
}
};
The only one I need to do is to rey=turn j
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n = nums.size();
int j = 0;
for(int i = 0; i < n; i++){
if (nums[i] != val){
nums[j] = nums[i];
j++;
}
}
for(int i = j; i < n; i++){
nums[i] = 1;
}
return j;
}
};