leetcode80 Remove Duplicates from Sorted Array II
2016-02-26 11:04
489 查看
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.
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,
2and
3.
It doesn't matter what you leave beyond the new length.
Subscribe to see which companies asked this question
package leetcode80; public class Solution { public int removeDuplicates(int[] nums) { return remove(nums, 2); } public int remove(int[] nums, int k){ int n = nums.length; if(n<k) return n; int cnt=1; int j=1; for(int i=1;i<n;i++){ if(nums[i]==nums[i-1]){ cnt++; if(cnt>k) continue; else{ nums[j++]=nums[i]; } }else{ cnt=1; nums[j++]=nums[i]; } } return j; } public static void main(String[] args) { // TODO Auto-generated method stub int[] nums = {1,1,1,2,2,3}; System.out.println(new Solution().removeDuplicates(nums)); } }
相关文章推荐
- 一个技术创业者的自白:你为什么想创业?
- 从头认识SpringBatch批处理框架--实例场景一信用卡消费对账
- 几道数学问题 (未完成)
- SSDB的介绍
- Android实现下载图片并保存到SD卡中
- multi-CPU, multi-core and hyper-thread--转
- Shell下变量比较
- Windows批处理 调用程序后 不等待子进程 父进程继续执行命令
- Session与Cookie
- Python基础回顾之-环境搭建及首次使用
- 1879: [Sdoi2009]Bill的挑战 状压DP
- 总结能力。
- 数据存储综合练习_javabean的介绍
- ios开发笔记(4)
- JAVA8之lambda表达式详解,及stream中的lambda使用
- if-else和try-catch
- buffer object,render buffer,framebuffer
- 无序集合(set)数据类型
- iOS storyboard 用法详解
- ios开发笔记(3)