100. Remove Duplicates from Sorted Array && 101. Remove Duplicates from Sorted Array II [easy]
这两题类似,所以放在一起,先看第一题:
Description
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.
Example
Given input array A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
给一个有序数组,去掉重复的部分,并返回不重复的数组长度。详细思路见代码中的注释,代码如下:
public class Solution {
/*
* @param nums: An ineger array
* @return: An integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if(nums.length==0)
return 0;
int cur=0;
int pre=0;
int n=nums.length;
while(cur<n){
if(nums[cur]==nums[pre]) cur++;//重复了就忽略
else nums[++pre]=nums[cur++];//没重复,则添加到pre序列中
}
return pre+1;//pre即新数组的最后元素的下标,因为返回长度,所以加一
}
}
再看另一题,有区别,个别细节不一样,但整体思路一样。代码如下:
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
if(nums.length==0){
return 0;
}
if(nums.length==1){
return 1;
}
//区别于前一题,确保元素大于等于两个时,cur=1
int pre=0;
int cur=1;
int n=nums.length;
while(cur<n){
//和前一题思路一样,什么时候忽略这个元素呢?只有等下标为pre和pre-1的元素都等于cur所指的元素时,说明队列中已经有两个该元素了,所以这个元素应该被忽略并覆盖,cur++
//当数组下标有减号时 例如 pre-1时,格外注意一下,数组下标不能小于0,否则会出错,即pre必须大于0
if(pre!=0&&nums[cur]==nums[pre]&&nums[cur]==nums[pre-1]){
cur++;
}else{
//如果不满足if条件,说明cur所指的元素是有价值的,添加到pre队列中。
nums[++pre]=nums[cur++];
}
}
return pre+1;
}
}
100. Remove Duplicates from Sorted Array && 101. Remove Duplicates from Sorted Array II [easy]的更多相关文章
- js 扩展Array支持remove方法
/* * 方法:Array.remove(dx) 通过遍历,重构数组 * 功能:删除数组元素. * 参数:dx删除元素的下标. */ Array.prototype.remove = function ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- Day07_39_集合中的remove()方法 与 迭代器中的remove()方法
集合中的remove()方法 与 迭代器中的remove()方法 深入remove()方法 iterator 中的remove()方法 collection 中的remove(Object)方法 注意 ...
- Array.apply(null, {length: 20})和Array(20)的理解
话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...
- 选择排序是外面循环的array[i]与内循环的array[j]比较。冒泡排序是内循环的相邻两个值做比较修改
选择排序是外面循环的array[i]与内循环的array[j]比较.冒泡排序是内循环的相邻两个值做比较修改
- [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 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 83. Remove Duplicates from Sorted List + 82. Remove Duplicates from Sorted List II
▶ 删除单链表中的重复元素. ▶ 83. 把重复元素删得只剩一个,如 1 → 1 → 2 → 3 → 3 → 3 → 4 → 5 → 5 变成 1 → 2 → 3 → 4 → 5.注意要点:第一个元素 ...
- [array] leetCode-27. Remove Element - Easy
27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...
随机推荐
- 用模板引擎Art-Template渲染空格或换行符引发的一场“命案”
一.绪论 说实话,真的不知道如何给这篇博客命名,因为我觉得应该有一些小伙伴遇到跟我同样的问题正在抓耳挠腮中. 二.导火索 最近在做一个移动H5翻页的功能,类似于MAKA模板那种.假设大致框架如下 ...
- Python 学习笔记(十一)Python语句(一)
运算符和条件语句 算术运算符 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 - 两个数相乘 ...
- Epub 阅读器 - iOS
因项目需求接触的 EPub 阅读器,前前后后尝试了很多库,最后找到了个相对兼容不错的展开了调试;其中对解压缩和数据加载方面进行了改造优化,使其更加的完美; 其大概原理是首先将 epub 文件解压后得到 ...
- 我的前端工具集(八)获得html元素在页面中的位置
我的前端工具集(八)获得html元素在页面中的位置 liuyuhang原创,未经允许禁止转载 目录 我的前端工具集 有时候需要用点击等操作,来获取某元素在页面中的位置,然后在该位置添加某些操作 如 ...
- AtomicIntegerArray数组类型类
前一篇文章学习了AtomicXXX基本数据类型类,可以为int,boolean或者reference类型,也就是单个元素的原子类.那么数组类型呢? 下面以AtomicIntegerArray为例进 ...
- Jquery实现简单图片轮播
html代码: <div class="show"> <div class="left"> <div class="sh ...
- linux下pip错误 ImportError: No module named 'pip_internal'
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate sudo python get-pip.py
- linux系统基础之六--系统引导(基于centos7.4 1708)
- Delphi的TValue探索(一)
TValue是Delphi的RTTI系统的重要类型. 经过摸索,发现TValue功能强大,可以实现很多功能.本文章中所有程序采用XE3运行通过. 一.TValue结构 TValue定义在System. ...
- Python学习笔记八:文件操作(续),文件编码与解码,函数,递归,函数式编程介绍,高阶函数
文件操作(续) 获得文件句柄位置,f.tell(),从0开始,按字符数计数 f.read(5),读取5个字符 返回文件句柄到某位置,f.seek(0) 文件在编辑过程中改变编码,f.detech() ...