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

时间:2022-12-04 14:34:48

题目链接: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. &lbrack;LeetCode&rsqb; 80&period; Remove Duplicates from Sorted Array II &star;&star;&star;&lpar;从有序数组中删除重复项之二&rpar;

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

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

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

  3. LeetCode 80 Remove Duplicates from Sorted Array II &lbrack;Array&sol;auto&rsqb; &lt&semi;c&plus;&plus;&gt&semi;

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

  4. &lbrack;leetcode&rsqb; 80&period; Remove Duplicates from Sorted Array II &lpar;Medium&rpar;

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

  5. &lbrack;LeetCode&rsqb; 80&period; 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. &lbrack;LeetCode&rsqb; 80&period; 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&period; Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

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

  8. &lbrack;leetcode&rsqb;80&period; Remove Duplicates from Sorted Array II有序数组去重&lpar;单个元素可出现两次&rpar;

    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. FileStream

    允许其他进程只读打开 New FileStream("路徑", FileMode.Append, FileAccess.Write, FileShare.Read) 允许其他进程以 ...

  2. Bootstrap之表单控件状态

    Bootstrap中的表单控件状态主要有三种:焦点状态,禁用状态,验证状态.   一.焦点状态:该状态告诉用户可输入或选择东西 焦点状态通过伪类“:focus”以实现. bootstrap.css相应 ...

  3. excel 两列比较内容是否相同

    C1列输入   =IF(A1=B1,"","不同") 然后下拉以比较其他行

  4. linux 下如何安装Telnet &quest;

    1 如何查看我的linux下是否安装了这个服务?2 没有安装的情况下,如何安装?3 client端 需要安装什么吗? 查看:rpm -qa | grep telnet安装:yum install -y ...

  5. 管理科学与工程 国内核心期刊 国外a刊及SCI

    国内: 管理科学与工程: 管理科学学报 A+   (匿名审稿,绝对牛刊,不比一般的SCi期刊的质量差) 系统工程理论与实践 A   (实名审稿,关系稿很多,尤其是挂编委的文章很多,但质量尚可)系统工程 ...

  6. this call和apply

    this指针总是指向一个对象,大致可以分为以下四种: 1,作为对象的方法调用(this指向该对象) 2,作为普通函数调用 当函数不作为对象的属性被调用时,也就是普通函数方式,此时的this总是指向全局 ...

  7. Zabbix 集成 OneAlert 实现全方位告警

    1. 前言 告警将重要信息发送给运维「或者其他相关人」,及时发现并且处理问题.在所有开源监控软件里面,Zabbix 的告警方式无疑是最棒的.告警的方式各式各样,从 Email 告警到飞信.139/18 ...

  8. &lbrack;转&rsqb;Install App to SD

    本文转自:http://www.douban.com/group/topic/29597344/ If you want to move more apps to the SD card, you'l ...

  9. JavaScript数组的学习

    1数组的创建 var arr1 = new Array(); var arr2=[1,2,3]; 2数组常用的方法: push,pop,shift,unshift,splice,slice,sort, ...

  10. springboot中,页面访问不到静态资源

    例一,静态资源放在默认的目录,如:resources/static或resources/templates 访问静态资源的时候,路径不应带上默认目录,因为springboot默认从这些目录下开始加载, ...