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 1122 and 3. It doesn't matter what you leave beyond the new length.

题目大意:给定一个排好序的数组,有重复元素,要求返回新数组长度,新数组中重复元素只能出现两次,新长度后面的是什么无所谓。

解题思路,从前向后遍历,记录一个newLen变量,重复元素记录两次,非重复元素就直接copy到新数组应有的下标处。

    public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int newLen=0;
for(int i=0;i<nums.length;i++){
int pos=i;
nums[newLen++]=nums[i];
while(i<nums.length-1&&nums[i]==nums[i+1]){
i++;
}
if(pos==i)
continue;
nums[newLen++]=nums[i];
}
return newLen;
}

Remove Duplicates from Sorted Array II ——LeetCode的更多相关文章

  1. Remove Duplicates from Sorted Array II [LeetCode]

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  3. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  7. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  8. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  9. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. 关于pv的那些事!!

    遗留问题:whid=1969的日志记录是什么意思? 网站站点信息未分配的时候,会用1969去代替站点信息. PV:页面浏览量(page view),用户每次打开或刷新一次网页即被计算一次. 关于pv的 ...

  2. css(display,float,position)

    display 用来设置元素的显示方式 display : block | none | inline | inline-block inline:指定对象为内联元素 block:指定对象为块元素 i ...

  3. WPF TextElement内容模型简介(转)

    本内容模型概述描述了 TextElement 支持的内容. Paragraph 类是 TextElement 的类型. 内容模型描述哪些对象/元素可以包含在其他对象/元素中. 本概述汇总了派生自 Te ...

  4. 文字排版--下划线(text-decoration:underline)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. javascript基础学习(四)

    javascript之流程控制语句 学习要点: 表达式语句含义 选择语句:if.if...else.switch 循环语句:while.do...while.for.for...in 跳转语句:bre ...

  6. 执行hadoop fs -ls时出现错误RuntimeException: core-site.xml not found

    由于暴力关机,Hadoop fs -ls 出现了下图问题: 问题出现的原因是下面红框框里面的东西,我当时以为从另一个节点下载一个conf.cloudera.yarn文件就能解决问题,发现不行啊,于是删 ...

  7. python sklearn模型的保存

    使用python的机器学习包sklearn的时候,如果训练集是固定的,我们往往想要将一次训练的模型结果保存起来,以便下一次使用,这样能够避免每次运行时都要重新训练模型时的麻烦. 在python里面,有 ...

  8. 24种设计模式--多例模式【Multition Pattern】

    这种情况有没有?有!大点声,有没有?有,是,确实有,就出现在明朝,那三国期间的算不算,不算,各自称帝,各有各的地盘,国号不同.大家还 记得那首诗<石灰吟>吗?作者是谁?于谦,他是被谁杀死的 ...

  9. javascript——集合类

    /** * Created by Administrator on 2015/4/14. */ function Set() { this.values = {}; this.n = 0; this. ...

  10. visual studio中验证控件的使用

    1.RequiredFieldValidator:验证一个必填字段,如果这个字段没填,那么,将不能提交信息. RequiredFieldValidator控件中,主要设置三个属性: (1)ErrorM ...