LeetCode & Q26-Remove Duplicates from Sorted Array-Easy
Descriptions:
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.For example,
Given input array nums =
[1,1,2],Your function should return length =
2, with the first two elements of nums being1and2respectively. It doesn't matter what you leave beyond the new length.
我写的一直有问题...用了HashSet集合,没有研究过这个类型,[1,1,2]输出结果一直是[1,1]
(问题一发现,在于,题目要求的是改变nums[]的内容,而不是输出一个新的数组)
(在小本本上记下,要研究HashSet)
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static int removeDuplicates(int[] nums) {
Set<Integer> tempSet = new HashSet<>();
for(int i = 0; i < nums.length; i++) {
Integer wrap = Integer.valueOf(nums[i]);
tempSet.add(wrap);
}
return tempSet.size();
}
}
下面是优秀答案
Solutions:
public class Solution {
public static int removeDuplicates(int[] nums) {
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != nums[j]) {
nums[++j] = nums[i];
}
}
return ++j;
}
}
有两个点需要注意:
- 因为重复的可能有多个,所以不能以相等来做判定条件
- 注意
j++和++j的区别,此处用法很巧妙,也很必要!
LeetCode & Q26-Remove Duplicates from Sorted Array-Easy的更多相关文章
- Leetcode 26. Remove Duplicates from Sorted Array (easy)
Given a sorted array, remove the duplicates in-place such that each element appear only once and ret ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [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% ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- [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] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [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] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- Java interview questions(No1)
1.什么是构造和析构方法?功能是? 答: 构造方法: 每个类至少有一个构造方法,类初始化时调用的方法 1.方法名和类名相同 2.无返回值类型 格式:访问权限 类名(参数列表) {}; 1.自己定义构造 ...
- 开发中使用mongoTemplate进行Aggregation聚合查询
笔记:使用mongo聚合查询(一开始根本没接触过mongo,一点一点慢慢的查资料完成了工作需求) 需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话.地址.支付总金额以及总 ...
- MacOS中升级openssl
MacOS中升级openssl ➜ ~ brew instal openssl 使用情况中始终发现,openssl并没有真正升级 在/usr/local/Cellar/openssl/目录中 ...
- 使用MBROSTool 工具制作本地硬盘F3救急模式的方法总结
前面写了一篇使用MBROSTool 工具制作本地硬盘多启动盘的方法总结.里面就是可以把一些系统安装到硬盘上面方便使用,比如安装PE到硬盘,不过启动的时候会先进入多UDm菜单,然后选择[启动本地系统]后 ...
- 使用netty的第一个Hello World
server端 package com.netty.test;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.Cha ...
- 简单docker镜像修改方式
• 创建Dockerfile,文件内容如下: FROM nps:v1.0.1 ENTRYPOINT ["/usr/bin/init.sh"] • 启动基础镜像:docker run ...
- 【日记】一次程序调优发现的同步IO写的问题,切记
众所周知,我们在写程序的时候,好习惯是在重要的代码打上日志.以便监控程序运行的性能和记录可能发生的错误. 但是,如果日志是基于同步IO文件操作,那么就必须考虑到访问总次数或并发数目. 如果总次数或并发 ...
- [poj3687]Labeling Balls_拓扑排序
Labeling Balls poj-3687 题目大意:给出一些球之间的大小关系,求在满足这样的关系下,编号小的尽量比编号大的球的方案. 注释:1<=N(球的个数)<=200,1< ...
- 关于SELECT LAST_INSERT_ID()的使用规则
尊重个人劳动成果,转载请注明出处: http://blog.csdn.net/czd3355/article/details/71302441 首先我先解释以下在在映射文件中的代码是什么意思. < ...
- (译文)学习ES6非常棒的特性-字符串常量基础
字符串常量基础 在ES2015之前我们是这么拼接字符串的: var result = 10; var prefix = "the first double digit number I le ...