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, ...
随机推荐
- 非常酷的CSS3垂直下拉动画菜单
昨天我向大家介绍了一款兼容性不错的jQuery淡入淡出下拉菜单,今天要分享一款相对绚丽的CSS3垂直下拉动画菜单,不过需要支持CSS3的浏览器才能有效果.下面是效果图,一起看看. 我们也可以在这里查看 ...
- Maven的pom.xml文件结构之基本配置parent和继承结构[转]
1.Maven项目的继承 Maven项目之间不仅存在多模块的聚合关系,而且Maven项目之间还可以存在相互继承的关系. Maven项目之间的继承关系通过<parent>表示,在子Maven ...
- nginx搭建(centos7)
1.安装前准备: 系统: CentOS 7.5 x64 下载包:wget yum -y install wget 安装: 2.安装一下这些依赖条件: yum install gcc pcre pcre ...
- tablediff工具实用
1. tablediff 是什么? tablediff 实用工具用于比较两个非收敛的表中的数据,它对于排除复制拓扑中的非收敛故障非常有用. 2. tablediff 用哪些用法? 1) . 在充当复制 ...
- 【LSTM】Understanding-LSTMs
http://colah.github.io/posts/2015-08-Understanding-LSTMs/
- WebJars are client-side web libraries (e.g. jQuery & Bootstrap) packaged into JAR (Java Archive) files
webjars网站https://www.webjars.org/,这里将很多的东西都打包成了jar包,想要用什么只需要导入相关的依赖就可以了. 比如springboot会用到jquery,webja ...
- redhat enterprise edition 6.8:禁止ipv6后,nfs文件系统无法挂载:no such device
如题:谨记. 附注:如何禁止ipv6? 方法一 第一种方法是通过 /etc/sysctl.conf 文件对 /proc 进行永久修改. 换句话说,就是用文本编辑器打开 /etc/sysctl.conf ...
- centos 上安装nodejs v8.0.0
新建目录www 下载nodejs wget https://npm.taobao.org/mirrors/node/v8.0.0/node-v8.0.0-linux-x64.tar.xz 解压 tar ...
- mysql中json_remove函数的使用?
需求描述: 今天看json记录,可以通过json_remove函数对一个key或多个key从个json记录中去掉. 操作过程: 1.查看一个已经存在的json表 mysql> select * ...
- php单元测试断言方法
1.assertArrayHasKey() 用法:$this->assertArrayHasKey('foo', ['bar' => 'baz']); 等同于array_key_exist ...