题目链接: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. Spring简单集成Redis

  2. php 多页面间共享变量

    1. 保存session的页面session1.php <? echo "hi, let's try session<br/>"; session_start() ...

  3. 一则Gedit死机引起的错误之解决

    一次,虚拟机中CentOS不明原因死机了,终端打开不能操作,SecureCRT和从SecureCRT中运行的托关于本机Windows下的Xming下的gedit也死掉了,无奈只能强制关机.重新启动后其 ...

  4. Array.prototype.removeBeginWithVal(删除数组内以某值开头的字符串对象)

    Array扩展方法: //author: Kenmu //created time: 2015-03-16 //function: 删除数组内以某值开头的字符串对象 Array.prototype.r ...

  5. extJS 中 ext.data 介绍

    ext.data 最主要的功能是获取和组织数据结构,并和特定控件联系起来,于是,Ext.data成了数据的来源,负责显示数据. Ext.data在命名空间中定义了一系列store.reader和pro ...

  6. 小波变换——哈尔小波,Haar

    哈尔小波转换是于1909年由Alfréd Haar所提出,是小波变换(Wavelet transform)中最简单的一种变换,也是最早提出的小波变换. Alfréd Haar,1885~1933,匈牙 ...

  7. srv.exe蠕虫病毒~

    你是否在电脑使用过程中遇到过这样的问题: 1.文件运行后,同目录下会出现一个原名 srv.exe的文件 2.文件运行后会把浏览器打开 3.电脑上的html文件末尾会增加一大堆东西 完了,电脑中了srv ...

  8. 【12月06日】A股全市场情绪指标整理分析

    1. A股全市场的股权质押比例 2018年11月30日,A股全市场,质押股数占全市场总股本数比:9.997%,最近2周出现了3.2%的轻微回落.同历史时期相比,仍然处于高位. 2. A股全市场的解禁市 ...

  9. linux中高亮显示文本的工具 -- bat

    bat 的项目地址 https://github.com/sharkdp/bat bat 是用rust 开发的, 在centos中安装bat需要rust的环境, 我们可以通过安装rust的包管理工具c ...

  10. Linux+Redis实战教程_day03_1、Redis-LinkedList【重点】

    1.redis-LinkedList[重点] Java List : 数组ArrayList 链表LinkedList 为什么redis选取了链表? Redis操作中,最多的操作是进行元素的增删 使用 ...