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. jQuery -- DOM节点的操作

    DOM 操作的分类: dom core: getElementById() getElementsByTagName() getAttribute() setAttribute() html-dom ...

  2. java之ubuntu12.04 开发环境配制

    配置java开发环境,即安装jdk: 1.配置环境变量 ,更改/etc/profile文件:sudo gedit /etc/profile; 在文件最后加上如下几行(其实跟windows下的配置原理一 ...

  3. Pressure-sensitive pores scattered

    Some researchers take issue with this however - a shark's vision is too good and its sense of smell ...

  4. 对contentoffset的理解

    今天遇到一个问题,在写瀑布流时,竖屏的时候可以正常实现,在手机变成横屏后,总是显示不全. 最终查了两个小时,查到了导致这个的原因,是自己的判断cell是否在当前显示区域的方法写错了. 根本原因是没有很 ...

  5. jquery中的children()和contents()的区别

    1.children()只会返回元素节点 2.contents()还可以返回文本节点

  6. Uncaught ReferenceError: XXX is not defined

    Uncaught ReferenceError: XXX is not defined 这个问题困扰我很久,虽然找到了解决方法,但是还不是很明白. 如下所示:是报错的代码. 如果把它改成下面的形式就可 ...

  7. fushioncharts 使用教程要点---使用JSON数据方式

    1.建立图表步骤: A.下载fushionChart,引入FusionCharts.js和FusionChartsExportComponent.js文件 B.建立图表对象 var chart1 =  ...

  8. 龙哥库塔法or欧拉法求解微分方程matlab实现

    举例:分别用欧拉法和龙哥库塔法求解下面的微分方程 我们知道的欧拉法(Euler)"思想是用先前的差商近似代替倒数",直白一些的编程说法即:f(i+1)=f(i)+h*f(x,y)其 ...

  9. NYOJY 491 幸运三角形

    描述 话说有这么一个图形,只有两种符号组成(‘+’或者‘-’),图形的最上层有n个符号,往下个数依次减一,形成倒置的金字塔形状,除第一层外(第一层为所有可能情况),每层形状都由上层决定,相邻的符号相同 ...

  10. 上传8m以上文件,报错误 101 (net::ERR_CONNECTION_RESET):连接已重置

    经过多方查找,原来是因为我使用了nginx反响代理的原因.nginx在做反向代理时,默认的可以上传的附件大小是1M,可以通过设置nginx.conf中的client_max_body_size进行更改 ...