[LeetCode] Remove Duplicates from Sorted Array II
Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3]
加一个变量记录一下元素出现的次数即可。这题因为是已经排序的数组,所以一个变量即可解
决。如果是没有排序的数组,则需要引入一个 hashmap 来记录出现次数。
方法1 用A[i] 和 A[index] 比较,同时,搞一个计数器
1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if(n == 0) 5 return 0; 6 7 int index = 0; 8 int cnt=1; 9 for(int i = 1; i<n; i++) 10 { 11 if(A[index] != A[i]) 12 { 13 index++; 14 A[index]=A[i]; 15 cnt = 1; 16 } 17 else 18 { 19 if(cnt ==1) 20 { 21 index++; 22 A[index]=A[i]; 23 cnt +=1; 24 } 25 } 26 27 } 28 return index + 1; 29 } 30 };
方法2 用A[i] 和 A[index-1] 比较,此方法可推广至最多允许k个数的情况
1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 if (n <= 2) return n; 5 int index = 2;//此方法可推广至最多允许k个数的情况,修改inde的值即可 6 for (int i = 2; i < n; i++){ 7 if (A[i] != A[index - 2]) 8 A[index++] = A[i]; 9 } 10 return index; 11 } 12 };
[LeetCode] Remove Duplicates from Sorted Array II相关推荐
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- LeetCode 80. Remove Duplicates from Sorted Array II
80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode之Remove Duplicates from Sorted Array II
1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- [LeetCode-JAVA] Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- 删除重复值(2以上)Remove Duplicates from Sorted Array II
2019独角兽企业重金招聘Python工程师标准>>> 问题: Follow up for "Remove Duplicates": What if duplic ...
- 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II
重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...
最新文章
- 03 在百度地图上定位到指定位置
- OpenExpressApp对建模支持的初步计划
- h3c防火墙u200配置命令_h3c 防火墙清除配置
- 重磅!“东方理工大学”来了!
- Shell——输入/输出重定向
- STM32工作笔记0049---JLINK在线调试__软件调试方法与技巧
- shell waite php,linux shell wait命令详解
- 恒丰银行助手提示注册表异常但修复不了的解决方法
- OpenGL调用GPU(七)
- win2012 定时自动备份mysql_SQL SERVER 2012数据库自动备份的方法
- 中国知名it软件开发外包公司有哪些呢
- OneLedger蓄势待发,引爆跨链热点
- 经济学基础(本)【4】
- 拳皇觉醒服务器维护,拳皇全明星拳魂觉醒手游9月26日更新公告_拳皇全明星拳魂觉醒9月26日更新了什么_玩游戏网...
- Markdown表格合并单元格
- 一张图理解线性空间,度量空间,赋范空间,巴拿赫空间,内积空间,欧几里得空间,希尔伯特空间
- 三极管构成的电流负反馈放大器
- 硬盘分区MBR和GPT知识详解
- pdf解密,pdf,jpg,word格式互相转换
- 视频编码技术 -1.2色彩原理
热门文章
- python 二维list取列
- 机器学习--线性代数基础
- C++中的位运算和|
- 2台xenserver组成的资源池开启HA存在的问题
- C语言实现一种简单的应用服务器内部数据结构的思路(三)
- [原创][连载].基于SOPC的简易数码相框 - Nios II SBTE部分(软件部分) - 从SD卡内读取图片文件,然后显示在TFT-LCD上...
- linux游戏欢迎界面,制作Linux登录欢迎界面
- ACM 模板--邻接表 无向图 搜索算法
- 找出数组中两个只出现一次的数字
- 时序图、活动图、状态图、协作图的区别