LeetCode 080 Remove Duplicates from Sorted Array II
2015-10-24 08:25
260 查看
题目描述
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.
代码
[code] public static int removeDuplicates(int[] nums) { int cur = 2; for (int i = cur; i < nums.length; i++) { //一个数字,最多出现2次 if (!(nums[i] == nums[cur - 1] && nums[i] == nums[cur - 2])) { nums[cur++] = nums[i]; } } return Math.min(cur, nums.length); }
相关文章推荐
- Spring事务配置的五种方式
- C++调用C函数,为什么要加extern "C"?
- zoj 2874 & poj 3308 Paratroopers (最小割)
- Android基础入门教程——8.3.3 Paint API之—— MaskFilter(面具)
- 今天走势将冲高回落后重新回归下跌周期
- 在线文档
- 自测你的肠道到底有多老
- matlab 有约束与无约束优化求解 optimization
- 第19讲----项目1 字母及其编码
- ajaxFileUpload.js 修复"unexpected token '<'" bug
- 为什么唱iOS 6.0选择Mantle
- 【从翻译mos文章】rac数据库,HC_<SID>.dat其他文件Oracle_Home用例下。
- HDU 1814 Peaceful Commission
- LeetCode 079 Word Search
- Eclipse+SVN
- Android studio 下出现"Cannot resolve symbol R"的解决方法
- python批量下载图片3
- 头文件没有一个全球性的定义
- Ubuntu 12.04 LTS 下安装LTIB
- java动态编程库,利用动态编程打印运行时调用全景(函数调用关系链)