class Solution { public: int removeElement(vector<int>& nums, int val) { if(nums.empty()) return 0; int l=0; int r=nums.size()-1; while(l<r){ if(nums[l]==val) { while(l<r&&nums[r]==val) { r--; if(l==r) break; } swap(nums[l],nums[r]); r--; } l++; } if(nums[r]!=val) r++; return r; } };
原文地址:https://www.cnblogs.com/lettleshel/p/9300797.html
时间: 2024-11-09 03:51:02