[Leetcode] Remove Duplicates from Sorted Array II
2015-11-30 14:45
337 查看
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums =
Your function should return length =
It doesn't matter what you leave beyond the new length.
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,
2and
3.
It doesn't matter what you leave beyond the new length.
public class Solution { public int removeDuplicates(int[] nums) { int outCount = 0; int repeatCount = 0; for (int i = 1; i < nums.length; ++i) { if (nums[i] != nums[outCount]) { nums[++outCount] = nums[i]; repeatCount = 1; } else { if (repeatCount != 2) { nums[++outCount] = nums[i]; repeatCount = 2; } else { continue; } } } return outCount + 1; } }
相关文章推荐
- 表连接(oralce、mysql、sql server)
- net.sf.json.JSONArray & net.sf.json.JSONObject
- 从程序员到CTO的Java技术路线图
- collectd python
- SSH框架之jar包讲解
- iOS开发常用正则表达式
- [TagCss]列表元素写法规范
- Mysql子查询的概念、分类、语法
- rectangle(暴力求解)
- maven 创建自定义archetype
- 【Leet Code】229. Majority Element II---Medium
- Spring整合mq
- DIV内容超出固定宽度部分用省略号代替
- C#的默认可访问性级别
- python20151130
- 2003外网ftp服务器搭建
- 如何开发一个App(Android)
- 如何开发一个App(Android)
- 如何开发一个App(Android)
- 如何开发一个App(Android)