Remove Duplicates from Sorted Array II 移除重复的元素(重)
来源:互联网 发布:人力资源软件销售招聘 编辑:程序博客网 时间:2024/05/12 03:11
题目:
点击打开链接
解答:
移除重复的元素 的升级版,思路和前面的题类似 用first指针指向最后一个排好序的元素,last指针指向第一个未处理的元素。
额外的,我们用dump变量来记录最后的first指针指向的元素重复了几次。
如果是新元素或者重复一次,则要添加到first后,重复两次的则继续last++;
初始化first = 0, last = 1 , dump = 1;表示已经处理完第一个元素。
代码:
class Solution { public:<span style="white-space:pre"></span> int removeDuplicates(int A[], int n) {<span style="white-space:pre"></span> int first = 0; //初始 = 0,有序数组为1 ,已经处理完第一个元素<span style="white-space:pre"></span> int last = 1; //未处理数组从1开始<span style="white-space:pre"></span> if (n == 0)<span style="white-space:pre"></span> return 0;<span style="white-space:pre"></span> int dump = 1; //已经处理完第一个元素,初始重复数 = 1<span style="white-space:pre"></span> while (last < n)<span style="white-space:pre"></span> {<span style="white-space:pre"></span> if (A[first] == A[last]) <span style="white-space:pre"></span> {<span style="white-space:pre"></span> if (dump == 2) //如果已重复两次 则忽略该元素<span style="white-space:pre"></span>++last;<span style="white-space:pre"></span> else //移动该元素,++dump<span style="white-space:pre"></span> {<span style="white-space:pre"></span> A[first+1] = A[last++];<span style="white-space:pre"></span> ++first;<span style="white-space:pre"></span> ++dump;<span style="white-space:pre"></span> }<span style="white-space:pre"></span> }<span style="white-space:pre"></span> else //出现了不相同的新元素,<span style="white-space:pre"></span> {<span style="white-space:pre"></span> A[first+1] = A[last++];<span style="white-space:pre"></span> ++first;<span style="white-space:pre"></span> dump = 1;<span style="white-space:pre"></span> }<span style="white-space:pre"></span> }<span style="white-space:pre"></span> return first + 1;<span style="white-space:pre"></span> } };
0 0
- Remove Duplicates from Sorted Array II 移除重复的元素(重)
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- LeetCode Remove Duplicates from Sorted Array 移除有序数列的重复元素
- [LeetCode-26] Remove Duplicates from Sorted Array(移除数组重复元素)
- 26. Remove Duplicates from Sorted Array(移除有序数组中的重复元素)
- (有序数组中移除重复元素)Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted List II 移除重复元素和其本身(重重)
- LeetCode | Remove Duplicates from Sorted Array II(删除重复的元素2)
- 【leetcode 移除有序序列重复数字】Remove Duplicates from Sorted Array(List) I(II)
- Leetcode 80. Remove Duplicates from Sorted Array II 重复移除 解题报告
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array——移除排序数组中重复元素
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Remove Duplicates from Sorted Array II 从有序数组里移除重复出现元素,最多重复出现2次 @LeetCode
- 三个线程,轮流打印10次各自的线程ID
- Project Euler 4
- fork函数详解(fork就是分叉的意思, 很形象)
- 套磁常见问题解答FAQ(转)
- 使用Python完成控制主机与任务节点的交互 [Demo]
- Remove Duplicates from Sorted Array II 移除重复的元素(重)
- ZOJ 3805 树形dp
- hdu 4983 Goffi and GCD(数论)
- 序列化(serialize)和反序列化(unserialize)以及__sleep()函数和__wakeup()函数
- GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流
- 使用GIT进行源码管理
- ,但由于计算机系统的故障(硬件故障、软件故障、网络故障、
- 详解Cocos2d-X中宏CC_DLL
- Windows+Git+TortoiseGit+COPSSH 安装图文教程