class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int size = nums.size();
if (size <= 0) {
return 0;
}
int start = 0;
int end = size - 1;
while (start < end) {
if (nums[start] != val) {
start++;
continue;
}
while (start < end && val == nums[end]) {
end--;
}
if (start < end) {
std::swap(nums[start], nums[end]);
end--;
}
}
return (val != nums[start]) ? (start + 1) : start;
}
};
leetcode移除元素
最新推荐文章于 2024-09-09 19:45:49 发布