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 being1
and2
respectively. 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 ...
随机推荐
- hadoop第一课
Hadoop基本概念 在当下的IT领域,大数据很"热",实现大数据场 景的Hadoop系列产品更"热". Hadoop是一个开源的分布式系统基础架构,由 Apa ...
- Linux初学者的总结
总算在年前把Linux的一些基本命令学完了,总结一下学到的(比较实用的)东西 我这里用的是Red Hat Enterprise Linux7的社区版本centos7 首先最重要的文件查看命令:ls.d ...
- 18.CSS
选择器 1.标签上直接设置style属性 <p style="color: red">直接标签里面写</p> 2.id选择器 <style> # ...
- Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
- 用Canvas生成随机验证码(后端前端都可以)
一 .使用前端生成验证码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- 关于在Editplus中设置内容提示比如syso的快捷输出的方法
在Editplus中默认的内容提示是很少的,比如我们最常用的syso快捷输出就没有,那么怎么来设置呢? 首先打开工具-首选项: 然后打开文件类型及语法-在文件类型中打开Java,如图: 然后打开 我们 ...
- 设计模式——外观模式(C++实现)
#include <string> #include <iostream> using namespace std; class STSystemA { public: voi ...
- pc端响应式-媒体查询
媒体查询(@media):能在不同的条件下使用不同的样式,使页面在不同在终端设备下达到不同的渲染效果 列举常用的pc屏幕宽度: 1024 1280 1366 1440 1680 1920 ...
- cxGrid_Q31584 cxgrid 拖放移动记录
cxgrid 拖放移动记录,cxgrid 拖放,cxgrid 拖动记录,cxgrid 鼠标拖动记录 这是cxgrid开发公司回复客户时所发送的源码项目,用于实现鼠标拖动记录,改变记录在表格中的位置,所 ...
- 使用guava实现找回密码的tokenCache以及LRU算法
源码包的简单说明: com.google.common.annotations:普通注解类型. com.google.common.base:基本工具类库和接口. com.google.common. ...