LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接: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 1
, 1
, 2
, 2
and 3
. It doesn't matter what you leave beyond the new length.
要求:1. 返回数组的长度
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(移除数组中出现两次以上的元素)的更多相关文章
- [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% ...
- **80. Remove Duplicates from Sorted Array II 删除排序数组中的重复项 II
1. 原始题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [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 ...
- [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 ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [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 ...
- lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...
随机推荐
- Sql Server数据库监听 c#代码
using AnfiniL.SqlServerTools.Data; using SqlServerTools; using SqlServerTools.Data; using System; us ...
- IntelliJ IDEA Default Keymap
Alt+回车 导入包,自动修正Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L 格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如get ...
- ★Wireshark基本介绍和学习TCP三次握手
之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括http,TCP,UDP,等网络协议包. 记得大学的 ...
- Android学习笔记——Intents 和 Intent Filters(二)
本人邮箱:JohnTsai.Work@gmail.com,欢迎交流讨论. 欢迎转载,转载请注明网址:http://www.cnblogs.com/JohnTsai/p/3993488.html 知识点 ...
- 制作做最小的fedora、ubuntu , jeos系统
之前做过, 2018年4月底,最新的fedora28 .ubuntu18.04发布后,自己又尝试做了下. ubuntu的成功了,比较简单: fedora的其实不用自己去制作,直接定制官方的Atomic ...
- Windows下使用最新的JDK1.7.0_51以上版本连接Jenkins出现SecurityException
我在slave节点上安装了jdk1.8, 当在节点上启动slave-agent的时候,报安全性限制的错误: java.lang.SecurityException: Missing required ...
- java学习之maven
maven是项目构建工具,能把项目抽象成POM(Project Object Model) Maven使用POM对项目进行构建.打包.文档化等操作 解决了项目需要类库的依赖管理,简化了项目开发环境搭建 ...
- Nine-patch
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch This NinePatch defi ...
- mysql 两台主主复制配置
A.服务器 [mysqld] # The TCP/IP Port the MySQL Server will listen on port= server-id= #master-host=10.1. ...
- 第六种方式,python使用cached_property缓存装饰器和自定义cached_class_property装饰器,动态添加类属性(三),selnium webdriver类无限实例化控制成单浏览器。
使用 from lazy_object_proxy.utils import cached_property,使用这个装饰器. 由于官方的行数比较少,所以可以直接复制出来用自己的. class cac ...