[LeetCode] Remove Duplicates from Sorted Array II
Well, an extension of Remove Duplicates from Sorted Array.
The program is fairly similar to that in this solution.
1 int removeDuplicates(vector<int>& nums) { 2 if (nums.size() <= 2) return nums.size(); 3 int pos = 1; 4 for (int i = 2; i < nums.size(); i++) 5 if (nums[i] != nums[pos] || nums[i] != nums[pos - 1]) 6 nums[++pos] = nums[i]; 7 return pos + 1; 8 }
After playing with it for a while, you may notice that the code can be easily extended to accommodate 3, 4, ... duplicates by modifying lines 3, 4, 5 in a similar manner.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步