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

For example,
Given sorted array A = [1,1,1,2,2,3],

Your function should return length = 5, and A is now [1,1,2,2,3].


题解:

设置两个变量:右边kepler和前向游标forward。如果当前kepeler所指的元素和它下一个元素相等,那么向A中存入这两个相等的元素,然后将forward置为kepeler+2,向前移动,如果碰到的元素都跟当前kepeler所指的元素相等,就忽略,否则,将kepeler移向forward所指元素,继续循环;如果当前kepeler所指的元素和它下一个元素不相等,那么就把这个元素直接加入A中,kepeler前移,继续循环。

例如题目中给的数组:1,1,1,2,2,3.

初始kepeler指向第一个1,发现kepeler+1也是一个1,就把forward置为kepeler+2=2,向前探测。探测到index为3的元素时候,发现和kepeler所指的元素不相等,就把kepeler移到索引为3处,继续上述过程,最终得到数组1,1,2,2,3.

代码如下:

 public class Solution {
public int removeDuplicates(int[] A) {
if(A == null || A.length == 0)
return 0;
int kepeler = 0,forward = 0;
int NewSize = 0; while(kepeler < A.length){
if(kepeler+1 < A.length && A[kepeler+1] == A[kepeler]){
A[NewSize++] = A[kepeler]; //相同的元素有两个,都放入数组中
A[NewSize++] = A[kepeler];
forward = kepeler+2;
while(forward < A.length && A[forward] == A[kepeler])
forward++;
kepeler = forward;
}
else if(kepeler < A.length){
A[NewSize++] = A[kepeler];
kepeler++;
}
}
return NewSize;
}
}

【leetcode刷题笔记】Remove Duplicates from Sorted Array II的更多相关文章

  1. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  2. 算法题丨Remove Duplicates from Sorted Array II

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? 示例 Giv ...

  3. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

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

  4. LeetCode(80)Remove Duplicates from Sorted Array II

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

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

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

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

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

  7. 【leetcode】Remove Duplicates from Sorted Array II

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

  8. 算法题丨Remove Duplicates from Sorted Array

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

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

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

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

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

随机推荐

  1. PHP-Manual的学习----【语言参考】----【类型】-----【float浮点型】

    笔记:1.浮点型(也叫浮点数 float,双精度数 double 或实数 real)可以用以下任一语法定义: <?php$a = 1.234; $b = 1.2e3; $c = 7E-10;?& ...

  2. windows10系统自带输入法不能切换中文如何解决

    具体如下: 1.打开计算机管理软件,右击“开始”按钮,在弹出的菜单中选择“计算机管理”: 2.或在桌面右击“此电脑”图标,在弹出的菜单中选择“管理”: 3.在打开的计算机管理软件中,选择“系统工具”- ...

  3. <转载> 为什么在Python里推荐使用多进程而不是多线程?

    经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢?                要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  4. python查询IP地址所属地

    1.linux命令行版 #!/usr/bin/python #-*- coding: utf-8 -*- import json import urllib import sys def get_da ...

  5. mybatis相关

    1 namespace dao中使用namespace+id一起来完成对mapper中sql statement的调用. 2 关于resultMap和parameterType parameterTy ...

  6. Java语言实现简单FTP软件------>上传下载管理模块的实现(十一)

    1.上传本地文件或文件夹到远程FTP服务器端的功能. 当用户在本地文件列表中选择想要上传的文件后,点击上传按钮,将本机上指定的文件上传到FTP服务器当前展现的目录,下图为上传子模块流程图 选择好要上传 ...

  7. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  8. linux后台开发必备技能

    一.linux和os: 1.命令:netstat tcpdump ipcs ipcrm  这四个命令的熟练掌握程度基本上能体现实际开发和调试程序的经验 2.cpu 内存 硬盘 等等与系统性能调试相关的 ...

  9. 【Prometheus】第二篇---基本查询语法

    这几天忙着开发别的项目,今天抽空写一下Prometheus的查询语法 1,查看指标 直接输入指标名字就行了,grafana会自动补全 2,查看某个指标的使用率,比如cpu使用率 用到rate函数,ra ...

  10. ubuntu切换到root

    sudo+命令,输入当前用户密码后以root权限执行命令,有时间限制且仅限当前命令. sudo -i,输入当前用户密码后以root权限登录shell,无时间限制.使用exit或logout退出. su ...