LeetCode OJ 80 Remove Duplicates from Sorted ArrayII
2015-07-05 17:55
639 查看
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.
思路:重复数可以达到两个,则在OJ26的基础上加一个flag标记重复数字即可
代码如下
运行时间:20ms
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.
思路:重复数可以达到两个,则在OJ26的基础上加一个flag标记重复数字即可
代码如下
class Solution { public: int removeDuplicates(vector<int>& nums) { int length = nums.size(); if(length<=2) return length; int index = 0; int flag=0; for(int i=1;i<length;i++) { if(nums[index]==nums[i]) { if(flag==0)//flag标记重复数的次数,当为0次时表示可以增加一个重复数 { nums[++index]=nums[i]; flag++; } else//达到重复上限 { flag++; } } else { flag=0; nums[++index]=nums[i]; } } return index+1; } };
运行时间:20ms
相关文章推荐
- yii1.1.15 accessRules的一个小bug
- yii下多条件多表组合查询以及自写ajax分页
- PHP代码
- 用php计算行列式
- php file_get_contents返回空 无效解决办法
- DedeAMPZ配置php的curl扩展
- 如何创建phpinfo查看php信息?
- PHP 错误与异常 笔记与总结(15 )使用观察者模式处理异常信息
- PHP常用功能模块
- ubuntu 14.04 安装php环境
- PHP生成唯一订单号
- php结合imgareaselect实现图片裁剪
- PHP json_encode出现乱码的问题
- php超快高效率统计大文件行数
- PHP版QQ互联OAuth示例代码分享
- PHP 获取ip地址代码汇总
- PHP中$_SERVER使用说明
- php实现短信发送代码
- php 字符串函数
- 【PHP】基础学习