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

感觉这道题的oa有点问题。。。先这样吧

public class Solution {
public int removeDuplicates(int[] nums) { int res=0;
int len=nums.length;
int set=0; for(int i=0; i<len; i++){
if(i==0){
set++;
res++;
}
else{
if(nums[i]==nums[i-1]){
set++;
if(set<=2){
res++;
}
}
else{
set=1;
res++;
}
} }
return res;
}
}

要in place 改变原数组:

public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0){
return 0;
}
int startPosition=0;
boolean isRepeated=false;
for(int i=1; i<nums.length; i++){
if(nums[i] != nums[startPosition]){
isRepeated=false;
startPosition++;
nums[startPosition] = nums[i];
}
else{
if(isRepeated == false){
startPosition++;
nums[startPosition]=nums[i];
isRepeated=true;
}
}
}
return startPosition+1; }
}

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

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

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

  2. [Leetcode] Remove Duplicates From Sorted Array II (C++)

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

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

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

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

  5. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  6. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  7. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

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

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

  9. 【leetcode】Remove Duplicates from Sorted Array II

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

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

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

随机推荐

  1. 【转】Memcached安装

    解析:Memcached是什么? Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. 一.软件版本    ...

  2. javafx之HTTP协议交互

    javafx端要获取获取如下信息: 服务器端获取的数据: javafx客户端发送的数据以及获取的数据: 工程目录: package Httputil; import IPsite.IPaddress; ...

  3. MyEclipse Java Build Path详解

    转载自:http://blog.163.com/magicc_love/blog/static/185853662201111161580631/ 1.设置"source folder&qu ...

  4. 如何在网页中调用百度地图api

    我想在木有提供地图接口的年代,前端工程师门要么只写上企业的具体地址,要么就是用一张标有自己位置的地图图片.但是现在不一样啦!为了增强用户体验,谷歌,甚至百度都很开放了,你可以在他们的网站上找到地图接口 ...

  5. StoryBoard解惑

    可以把StoryBoard看做是一组viewController对应的xib,以及它们之间的转换方式的集合.在StoryBoard中不仅可以看到 每个ViewController的布局样式,也可以明确 ...

  6. Go! new Hello World, 我的第一个Go程序

    以下语句摘自百度百科: Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进 ...

  7. @property? @property?说说他身后的事情。

    @property 后面可以有哪些修饰符? 1.线程安全的       aaomic.nonatomic 2.访问权限的       readonly.readwrite 3.内存管理(ARC) as ...

  8. C++Promise函数

    Promise内部会建立一个shared state是用来放一个相应的类型的值或是一个异常,并可被future object 取其数据当线程结果 promise是在形成成果后才将结果放进shared ...

  9. docker 源码分析 三(基于1.8.2版本),NewDaemon启动

    本文来分析一下New Daemon的启动过程:在daemon/daemon.go文件中: func NewDaemon(config *Config, registryService *registr ...

  10. 【杂】孔明锁6根解法 & 九连环的拆卸方法及还原

    **************************** Part1: 孔明锁6根解法: **************************** 第一步,编号: 第二步,按照编号组装: 第三步,完成 ...