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. EF实体框架数据操作接口(转)

    //----------------------------------------------------------------// Copyright (C) 2013 河南禄恒软件科技有限公司 ...

  2. SharePoint Document Library中的"Versioning Settings"功能与CSOM的对应

     博客地址:http://blog.csdn.net/FoxDave SharePoint文档库关于版本的设置:"Versioning Settings",可以通过CSOM用L ...

  3. jpa遇到的 org.hibernate.PersistentObjectException: detached entity passed to persist异常

    jpa遇到的 org.hibernate.PersistentObjectException: detached entity passed to persist异常 发生这个原因是因为我们已经在实体 ...

  4. ENTITYFRAMEWORKCORE 二使用配置文件来配置数据库链接

    首先 配置文件现在已经变成appsettings.json, 先添加一个连接字符串 "ConnectionStrings": { "PWDatabase": & ...

  5. 关于JAVA的数据转换总结

    数据转换在编程里面是十分常用的,将平常可能用到的数据转换类型总结起来会在以后码代码的过程中有很大帮助. 在数据转换之前,需要明白的是基础数据类型的自动转换和强制转换.接下来就先从数据类型的容量讲起. ...

  6. system_call中断处理过程分析

    本文所有的分析内容都是基于Linux3.18.6内核,鉴于对应不同内核版本,系统调用的实现不相同.若需要分析其他版本内核的系统调用的实现过程,请谨慎参考. system_call函数的功能是用来响应外 ...

  7. C#知识体系(二)用案例来理解委托与事件

    上一篇博客讲到了LinQ和lambda的常用方法 还有很多我们未知但c#设计团队已经为我们封装好的类和方法.随着我们不断的熟悉C#语言,渐渐的就会接触到其他的知识点,委托.事件.反射.线程.同步,异步 ...

  8. 准备再次开始更新文章,做了10年.net,有项目需要转java

    不仅要转java,而且还要直接上liferay portal ,一下子要学好多.

  9. WP7、WP8 格式化时间为距当前多少时间

    方法一: 使用 toolkit的 RelativeTimeConverter,使用方式 <phone:PhoneApplicationPage.Resources> <toolkit ...

  10. Source Insight 常用设置和快捷键大全

    Source Insight 常用设置和快捷键大全 退出程序 : Alt+F4 重画屏幕 : Ctrl+Alt+Space 完成语法 : Ctrl+E 复制一行 : Ctrl+K 恰好复制该位置右边的 ...