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.


题目标签:Array
  这道题目和之前的区别就是,可以保留第二个重复的数字。基本想法都和前一题一样,在这里只要多加一个int count 来记录这是第几个重复的number,如果是第二个的话,把pointer 往右移动一格,并且把nums[i] 的值 复制到 nums[pointer],然后count++。除此之外,还需要在遇到不同数字的情况里,加上,count = 1, 因为一旦遇到不同的数字,那么count 计数又要重新开始了。详细可以看代码,和之前那题代码的比较。
 
 
 

Java Solution:

Runtime beats 27.80%

完成日期:07/29/2017

关键词:Array

关键点:多设一个int count 来记录出现重复数字的次数

 public class Solution
{
public int removeDuplicates(int[] nums)
{
if(nums.length <= 2)
return nums.length; int pointer = 0;
int count = 1; for(int i=1; i<nums.length; i++)
{
// if this number is different than pointer number
if(nums[i] != nums[pointer])
{
pointer++;
nums[pointer] = nums[i];
count = 1;
}
else // if this number is same as pointer number
{
if(count == 1) // if it is second same number
{
pointer++;
nums[pointer] = nums[i];
count++;
}
}
} return pointer + 1;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)的更多相关文章

  1. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  2. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  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] 80. Remove Duplicates from Sorted Array II (Medium)

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

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

  9. Leetcode#80 Remove Duplicates from Sorted Array II

    原题地址 简单模拟题. 从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去 代码: int removeDuplicates(int A[], int n) { ) return n; ; ...

随机推荐

  1. geyear和getfullyear的区别

    getYear(),IE中获得正确年份,但有的浏览器获取的是当前年份-1900的值.而IE是当大于2000时,直接加上1900 getFullYear(),都是可以获得正确年份. 所以建议使用getF ...

  2. dup和dup2详解

    C语言中dup和dup2函数的不同和使用 发表时间: 2012年11月15日 | 作者: 陈杰斌 | 所属分类: C语言 | 评论: 0 | 浏览: 1024 在unix高级编程中有介绍dup和dup ...

  3. Spring第五篇【cglib、手动实现AOP编程】

    前言 到目前为止,已经简单学习了Spring的Core模块.也会怎么与Struts2框架进行整合了-.于是我们就开启了Spring的AOP模块了-在讲解AOP模块之前,首先我们来讲解一下cglib代理 ...

  4. Spring - Spring容器概念及其初始化过程

    引言 工作4年多,做了3年的java,每个项目都用Spring,但对Spring一直都是知其然而不知其所以然.鄙人深知Spring是一个高深的框架,正好近期脱离加班的苦逼状态,遂决定从Spring的官 ...

  5. Linux 环境下 MySQ导入和导出MySQL的sql文件

    将服务器上的文件导入或导出还需要使用工具传输到本机中,推荐使用winscp,与xshell搭配使用 1 导入数据库 两种方法 .首先建空数据库 mysql>create database abc ...

  6. angularjs的几种常见写法

    学习angularjs不久,遇见的angularjs的写法也是很多的感觉,今天就在这里记录一下,还有没见过的,继续学习中... angularjs 常用的几种种写法 1.链式: angular.mod ...

  7. 【 js 基础 】关于this

    this 关键字是 Javascript 中很特别的一个关键字,被自动定义在所有函数的作用域中.this提供了一种更优雅的方式隐式"传递"一个对象的引用.今天就来说说 this 的 ...

  8. html5新特性与HTML的区别

    * HTML5 现在已经不是 SGML 的子集,主要是关于图像,位置,存储,多任务等功能的增加. 绘画 canvas; 用于媒介回放的 video 和 audio 元素; 本地离线存储 localSt ...

  9. jenkins~集群分发功能和职责处理

    jenkins的多节点集群 在进行自动化部署时,你可以按着它们的项目类型去进行分别部署,这样即可以达到负载均衡,又可以达到一种职责的明确,比如像java的项目你可以使用linux服务来进行部署(拉代码 ...

  10. Hive基础(3)---Fetch Task(转)

    我们在执行hive代码的时候,一条简单的命令大部分都会转换成为mr代码在后台执行,但是有时候我们仅仅只是想获取一部分数据而已,仅仅是获取数据,还需要转化成为mr去执行吗?那个也太浪费时间和内存啦,所以 ...