博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Duplicates from Sorted Array II
阅读量:4074 次
发布时间:2019-05-25

本文共 869 字,大约阅读时间需要 2 分钟。

Remove Duplicates from Sorted Array II

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]

Java代码:

public class Solution {    public int removeDuplicates(int[] A) {    Integer prev = null; // last value    int count = 0; // number of continuous occurrences of last value    int index = 0; // points to index to place next qualified element    for (int i = 0; i < A.length; i++) {        int candidate = A[i];        if (prev != null && prev == candidate && count == 2)            continue;        A[index++] = A[i];        // reset count if a new value is encountered        if (prev == null || candidate != prev) {            prev = candidate;            count = 1;        } else            count++;    }    return index;}}
 

转载地址:http://jiuni.baihongyu.com/

你可能感兴趣的文章
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>
S3C2440中对LED驱动电路的理解
查看>>
《天亮了》韩红
查看>>
Windows CE下USB摄像头驱动开发(以OV511为例,附带全部源代码以及讲解) [转]
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>