No.026:Remove Duplicates from Sorted Array
问题:
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 being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.
官方难度:
Easy
翻译:
给定一个已经排序的数组,删除重复元素,返回新数组的长度。程序不关心这个长度之后存储的是什么东西。
例子:
给定数组:[1,1,2]
返回长度:2,且数组以[1,2]开头。
- 有2个很关键的信息:数组有序,不关心长度之后的内容。
- 不关心长度之后的内容,题目暗示了,在原数组上处理数据的内容。
- 由于数组有序,维护一个快指针(从第2项开始),一个慢指针(从第1项开始)。快指针遍历数组,判断两个指针指向的数字是否相同。若相同,不做处理;若不同,慢指针向前移一位,且快指针的值覆盖慢指针的值。
- 结束循环,返回长度为慢指针+1。
- 入参检查,同时,长度小于2的直接返回。
解题代码:
public static int removeDuplicates(int[] array) {
if (array == null) {
throw new IllegalArgumentException("Input error");
}
if (array.length < 2) {
return array.length;
}
// 慢指针
int i = 0;
for (int j = 1; j < array.length; j++) {
// 快指针不断前进,遇到与慢指针数字相同的情况,跳过
if (array[j] != array[i]) {
i++;
array[i] = array[j];
}
}
// 返回新数组的长度,不关心之后的内容
return i + 1;
}
removeDuplicates
相关链接:
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
PS:如有不正确或提高效率的方法,欢迎留言,谢谢!
No.026:Remove Duplicates from Sorted Array的更多相关文章
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array
题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear onl ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode OJ:Remove Duplicates from Sorted Array(排好序的vector去重)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- leetcode解题报告(1):Remove Duplicates from Sorted Array
描述 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II
题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...
随机推荐
- 让Chrome 接管邮件连接,收发邮件更方便了
页面中除了传统的超链接外,还可以将邮箱地址写入<a>标签,意思不表自明,当然是用户点击后就会打开相应的邮件客户端向这个连接指向的邮件地址发邮件. <a href="mail ...
- 复习sql第三次
1.层次型数据库以"树"结构表示数据库中数据间的关系:网状型以"图"结构表示数据库中数据间的关系:关系型数据库以"二维表"结构表示数据库中数 ...
- SSIS Destination 组件使用Fast-Load mode出错
查看一个Package的历史Message 数据,发现 DataFlow Task 经常出错,错误信息的Description是: Description: "While reading c ...
- Sql Server系列:Delete语句
数据的删除将删除表的部分或全部记录,删除时可以指定删除条件从而删除一条或多条记录.如果不指定删除条件,DELETE语句将删除表中全部的记录,清空数据表. 1 DELETE语法 [ WITH <c ...
- LINQ系列:LINQ to SQL Select查询
1. 查询全部字段 using (NorthwindContext context = new NorthwindContext()) { var expr = context.Products; f ...
- OpenCASCADE Performance Test
OpenCASCADE Performance Test eryar@163.com Abstract. Use the Draw Test Harness to test the performan ...
- 【原创】开源Math.NET基础数学类库使用(01)综合介绍
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- Neutron 如何支持多种 network provider - 每天5分钟玩转 OpenStack(70)
Neutron 的架构是非常开放的,可以支持多种 network provider,只要遵循一定的设计原则和规范.本节我们将开始讨论这个主题. 先讨论一个简单的场景:在 Neutorn 中使用 lin ...
- 对来自于Azure的远程连接文件(.rdp)的另一种更便捷的自定义方法
在上一篇日志中(很抱歉那张比较黑的截图)介绍了如何获得Azure中的Windows虚拟机的远程连接文件,以及一种基于文本编辑方式进行自定义的方法. 实际上对于在Windows下的用户来说,我们可以使用 ...
- ZOJ Problem Set - 1109 Language of FatMouse
这道题目最让人头疼的就是该题的input怎么结束,因为它要求输入一个空行的时候则一串字符串输入结束,这就不得不让人绕个弯来解决这个问题. (注:本人习惯于使用C中的字符串操作,但是用到map要求使用s ...