LeetCode(80) Remove Duplicates from Sorted Array II
2016-01-19 11:08
375 查看
Follow up for “Remove Duplicates”:
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
Subscribe to see which companies asked this question
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3],
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length.
Subscribe to see which companies asked this question
public int removeDuplicates(int[] nums) { boolean flag = true; int index = 0; if(nums.length == 0) return 0; for(int i = 1; i < nums.length; i++){ if(nums[index] != nums[i]){ nums[++index] = nums[i]; flag = true; } else if(flag == true){ nums[++index] = nums[i]; flag = false; } } return index+1; }
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length <= 2) return nums.length; int index = 2; for(int i = 2; i < nums.length; i++){ if(nums[i] != nums[index-2]) nums[index++] = nums[i]; } return index; } }
相关文章推荐
- [从头学数学] 第51节 两位数乘两位数
- 深入浅出RxJava四-在Android中使用响应式编程
- 各种环境变量的配置
- win7搭建WAMP
- Swift中的闭包(Closure)
- 【转】iOS开发4:关闭键盘
- 2000~2015年,房地产投资总额以及增长率
- Nginx正向代理与反向代理
- 微积分经典概念:极限、连续与函数
- UIScrollView的delaysContentTouches与canCancelContentTouches属性
- strcpy(字符串复制)和memcpy(内存复制)
- 浅谈 JS对数值处理的5个API
- Ubuntu 15.04 设置thin1.6.4作为Rails4.2.5的默认服务器
- c++ --> sizeof()使用小结
- MDK的优化应用
- HBase数据的导入和导出
- 树形结构的处理——组合模式(四):透明组合模式与安全组合模式
- 栈
- Linux下Tomcat重新启动
- 通过推送消息控制Android系统锁屏、唤醒