乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array
一、前言
我们这次的实验是去除重复的有序数组元素,有大体两种算法。
二、Remove Duplicates from Sorted Array
2.1 问题



题目大意理解,就是对数组进行元素去重,然后返回去处重复之后的长度,无论我们对数组做了什么的修改,都没有关系的,只要保证再返回的长度之内的数组正确性即可。因为最后是根据长度来遍历的,因此我们不用担心。
2.2 分析与解决
题目说的很清晰了,因此,我们首先想到了笨办法,那就是从左往右遍历,如果发现相等的元素,就将后面的元素集体前移,这样最差的时间复杂度是O(n~2)。因此我们想想有没有简单的方法,于是我们想到了只用找一个指针在前面开路,遇到不同的元素了,将这个元素填到相应的位置,然后继续搜索,直至遍历完所有的元素即可,这样一次遍历就能解决问题,时间复杂度O(n~2),只不过也留下了脏空间,不过题目说了不用我们管了,于是问题解决。
第一种是笨办法:
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
第二种是一次遍历:
public class Solution {
/**
*
* 题目大意
* 给定一个排序的数组,将数组中的重复元素去掉,相同的只保留一个作为新数组的元素,
* 并且返回数组新的元素个数,
* 不要创建一个新的数组来保存结果。在常量时间内解决这个问题
*
* 解题思路
* 从第二个元素开始处理,记为当前处理的元素,如果当前元素与他的前一个元素相同就删除这个元素,
* 如果不同就将它移动到正确的位置,返回最后数组元素个数。
*/
public int removeDuplicates(int[] A) {
if (A.length == 0) {
return 0;
}
int index = 0;//[0,index]只记录数组中出现的按从小到大的唯一一个数,已经排好序了
int next = 1;
// 算法思想:找index之后的比A[index]大的数,如是找到就移动到A[index+1]处,
// index移动到下一个位置,next移动到下一个位置,再找比A[index]大的数
while (next < A.length) {
while (next < A.length && A[index] == A[next]) { // 找不等于数组中最
next++;
}
if (next < A.length) {
index++;
A[index] = A[next];
next++;
}
}
return index + 1;
}
}

三、总结
将O(n~2)的复杂度下降一个等级,其实就是简单的利用了一些技巧和思维,但是节省而来的大量的运算时间。
乘风破浪:LeetCode真题_026_Remove Duplicates from Sorted Array的更多相关文章
- 乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array
乘风破浪:LeetCode真题_033_Search in Rotated Sorted Array 一.前言 将传统的问题进行一些稍微的变形,这个时候我们可能无所适从了,因此还是实践出真知, ...
- 【算法】LeetCode算法题-Remove Duplicates from Sorted Array
这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...
- 乘风破浪:LeetCode真题_004_Median of Two Sorted Arrays
乘风破浪:LeetCode真题_004_Median of Two Sorted Arrays 一.前言 说到算法,最难的就是一些需要通过分析得到一些递推公式或者有用的结论,进而用来解决问题的方法了. ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
随机推荐
- Spring AOP切面变成——创建增强类
说明 Spring使用增强类定义横向逻辑,同时Spring只支持方法连接点,增量类还包含在方法的哪一点添加横切代码的方位信息.所以增强既包含横向逻辑,又包含部分连接点的信息. 类型 按着增强在目标类方 ...
- win10 磁盘占用高--- 禁用用户改善反馈 CompatTelRunner.exe
1. 2.右键点开[这台电脑],点[管理],点[服务和应用程序]点[服务],在右边框里把[superfetch] [windows search][HomeGroupListener] [HomeGr ...
- [转]小程序web-view组件
本文转自:https://www.cnblogs.com/-nothing-/p/7910355.html 1,web-view这个组件是什么鬼? 官网的介绍:web-view 组件是一个可以用来承载 ...
- C#取整函数Math.Round、Math.Ceiling和Math.Floor 【非原创,用来收藏,分享】
1.Math.Round:四舍六入五取偶 引用内容 Math.Round(0.0) //0Math.Round(0.1) //0Math.Round(0.2) //0Math.Round(0.3) / ...
- 使用Docker调试Asp.Net Core
使用 Docker 进行部署 目前还是使用将发布出来的文件打包进docker镜像的形式 $ docker build -t pims . $ docker run --name pims --rm - ...
- sql语句将查询的结果拼接成字符串
表样: sqlserver: --方法1 DECLARE @STR VARCHAR(8000) SELECT @STR=ISNULL(@STR+',','')+userID FROM (SELECT ...
- 吉哥系列故事——临时工计划(dp)
吉哥系列故事——临时工计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- IDEA创建Struts2报错——web.xml
这里记录一个问题,用IDEA创建Struts2时会出现的错误,cannot resolve class or package ‘filter’,出现在web.xml文件中,不修改这个,那么你配置好了T ...
- python正则表达式3-模式匹配
re.S,使 '.' 匹配换行在内的所有字符 >>> pattern=r'ghostwu.com' >>> import re >>> re.f ...
- POJ3693(SummerTrainingDay10-J 后缀数组)
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10241 Ac ...