题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description

给定一个已经排好序的数组,数组中元素存在重复,如果允许一个元素最多可出现两次,求出剔除重复元素(出现次数是两次以上的)后的数组的长度。

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

要求:1. 返回数组的长度

         2. 确保上述长度内的数组为满足条件的元素,上述长度之外数组所保存的数字无关
 
很简单了,也就是判断每个数字出现了多少次就行了。不够两次的放到数组的新位置中,超过两次的不考虑。
 
给一个很笨的方法,这个方法实在是太low啦,不过为了突出很diao的那个方法有多么diao,还是贴出来啦。
不喜欢low low代码的请自动忽略,在此已将代码折叠。
 
package leetcode_100;

import java.util.HashMap;
import java.util.Map;
/***
*
* @author pengfei_zheng
* 移除数组中出现两次以上的元素
*/
public class Solution80{
public int removeDuplicates(int[] nums) {
int len = nums.length;
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("total",0);
for(int i = 0 ; i < len ; i ++){
int temp = map.get("total");
if(!map.containsKey(nums[i]+"")){
map.put(nums[i]+"",1);
nums[temp++]=nums[i];
map.put("total",temp);
}
else if(map.containsKey(nums[i]+"")&&map.get(nums[i]+"")<2){
map.put(nums[i]+"",2);
nums[temp++]=nums[i];
map.put("total",temp);
}
}
return map.get("total");
}
}

low low的代码

好的,还请忽略上述代码,请欣赏下面的代码:

public class Solution{
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}
}

这才是正确的姿势

LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)的更多相关文章

  1. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  2. **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II

    1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...

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

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

  4. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  8. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II

    题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...

随机推荐

  1. Console程序后台运行

    [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPt ...

  2. Intellij Idea反向生成Hibernate实体类

    每次根据数据库的表反向生成实体类老不记得步骤...脑子不够用,这里特意记录一下.碰到的问题也及时更新到这里来. 1. 工程添加Hibernate支持 两种方式: 第一种:工程上右键选择 "A ...

  3. php json_decode无法解析特殊问好字符

    在通过别人接口请求信息的时候,偶尔会遇到由于部分字符,如以下情况,则通过json_decode是会返回null的 但是这种情况通常不是由于整体编码的问题,因为在解析的时候就是以utf-8的编码解析的 ...

  4. 安卓开发笔记——WebView组件

    我们专业方向本是JAVA Web,这学期突然来了个手机App开发的课设,对于安卓这块,之前自学过一段时间,有些东西太久没用已经淡忘了 准备随笔记录些复习笔记,也当做温故知新吧~ 1.什么是WebVie ...

  5. mysql 错误代码:1118解决方法

    错误描述: 错误代码: 1118Row size too large. The maximum row size for the used table type, not counting BLOBs ...

  6. Grunt--Less

    摘要: 之前介绍了自动构建工具Grunt,其中有一个模块是"grunt-contrib-less",下面是配置Grunt自动编译less文件. 安装: Grunt是基于node,功 ...

  7. selenium+phantomjs渲染网页

    from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapa ...

  8. pytesseract 报windows err no2的错误

    需要把源安装文件pytesseract.py的修改为,tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe' 原始是t ...

  9. python--文件I/O--11

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 一.打印到屏幕 最简单的 ...

  10. 删除ORACLE目录OCI.dll文件无法删除 (转)

    删除ORACLE目录OCI.dll文件无法删除 今天准备把虚拟机里的10g卸载安装11g来研究一些新特性 卸载没有用自带的UnInstall工具之前看warehouse的讲课视频凭记忆手动卸载了下删除 ...