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. JAVA第二次作业展示与学习心得

    JAVA第二次作业展示与学习心得 在这一次作业中,我学习了复选框,密码框两种新的组件,并通过一个邮箱登录界面将两种组件运用了起来.具体的使用方法和其他得组件并没有什么大的不同. 另外我通过查阅资料使用 ...

  2. Windows 10 RTM 官方正式版

    Windows 10 各版本区别: Windows 10 家庭版:供家庭用户使用Windows 10 专业版:供小型企业使用 在家庭版基础上增加了域账号加入.bitlocker.企业商店等功能Wind ...

  3. B2C电子商务基础系统架构解析(转载)

    系统的开发与演化,前台严格follow消费者的购买流程,后台则盯牢订单流转,牢牢抓住这两条主线,才能高屋建瓴的看清B2C的逻辑链和数据流,更深刻的规划功能模块,从而更有效支撑实际业务的流转. 前台 前 ...

  4. EF的增删改查

    //获取分组信息        public List<UserGroupLogSys> GetUserGroupLogSyslist(int pageIndex, int pageSiz ...

  5. Alpha版使用说明书

     游戏规则:             玩家是黑色的小煤球哦!             通过方向键或者ASDW,来控制小球移动(上.下.左.右).             累计时间,直到碰到了红色的小球 ...

  6. MVC 请求处理流程(一)

    路由系统先获取路由数据,在实现了IHttpModule接口的UrlRoutingModule对象中通过注册HttpApplication的PostResolveRequestCache来解析路由数据并 ...

  7. 北大poj-2688

    Cleaning Robot Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4395   Accepted: 1763 De ...

  8. GnuRadio Hacking①:使用GnuRadio+SDR破解固定码无线遥控

    0×01 信号捕获 在这篇文章中,我们将使用GnuRadio+SDR硬件对某品牌型号的无线跳蛋进行无线重放攻击的演示. 市面上常见的无线遥控工作的频段,通常工作在315Mhz.433Mhz,也有少数的 ...

  9. 项目中 poi 导出 出现html特殊符号的实体 (已解决)

    导出excel 时出现 类似这样的>  符号 , 大概是存到数据库也是这样,然后jsp解析可以解析出来,但是java不认得,需要个人写出解析方法. 废话不说,贴码: /** *转换html特殊符 ...

  10. 我们正在等待一次技术革命的到来; We are waiting for the arrival of a technological revolution

    In the future, there must be a significant technological revolution just like Industrial Revolution. ...