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. [07] String字符串

    1.相同又不同的字符串 String str1 = new String("String"); String str2 = "String"; String s ...

  2. VS问题

    ref:https://q.cnblogs.com/q/86096/

  3. 百度编辑器不能插入html标签解决方法

    找到此方法: me.addInputRule(function (root) { var allowDivTransToP = this.options.allowDivTransToP; var v ...

  4. 随便讲讲我对于svn和git的想法

    1.SVN是集中式版本管理工具,而Git是分布式版本管理工具,这是核心区别. 二者都有集中的库,只是git偏向于分布式,用户可以再自己电脑上克隆一份自己的库,即使在断网的情况下也能够查看版本,创建分支 ...

  5. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  6. 三大修饰符static,final,abstract,接口和抽象类的区别

    package com.cityhero.test; public class ThreeModifier { //static静态的 // 概念:static可以修饰方法和属性,被static修的方 ...

  7. java初学者(新手)应该如何选择学习教材与网站

    作者:天天向上 1.学习教材选择推荐<JAVA核心技术>,想多看点代码多练习可以找<java开发实战经典>&amp;amp;lt;img src="https ...

  8. 如何写一个jquery插件

      本文总结整理一下如何写一个jquery插件?虽然现今各种mvvm框架异常火爆,但是jquery这个陪伴我们成长,给我们带来很多帮助的优秀的库不应该被我们抛弃,写此文章,作为对以往欠下的笔记的补充, ...

  9. JSP入门 Filter

    Filter,它的名字是过滤器,可以批量拦截修改servlet的请求和响应. 需要实现接口Filter 配置 <filter> <filter-name>EncodingFil ...

  10. linux上搭建ftp

    linux上搭建ftp 重要 解决如何搭建ftp         解决用户指定访问其根目录         解决访问ftp超时连接         解决ftp主动连接.被动连接的问题 1.安装ftp ...