这两题类似,所以放在一起,先看第一题:

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]的更多相关文章

  1. js 扩展Array支持remove方法

    /* * 方法:Array.remove(dx) 通过遍历,重构数组 * 功能:删除数组元素. * 参数:dx删除元素的下标. */ Array.prototype.remove = function ...

  2. 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 ...

  3. Day07_39_集合中的remove()方法 与 迭代器中的remove()方法

    集合中的remove()方法 与 迭代器中的remove()方法 深入remove()方法 iterator 中的remove()方法 collection 中的remove(Object)方法 注意 ...

  4. Array.apply(null, {length: 20})和Array(20)的理解

    话说今晚在学习Vue.js教程里:Render函数,这一章节是发现了一个问题,就是利用下面的这个render函数可以渲染20个重复的段落: render: function (createElemen ...

  5. 选择排序是外面循环的array[i]与内循环的array[j]比较。冒泡排序是内循环的相邻两个值做比较修改

    选择排序是外面循环的array[i]与内循环的array[j]比较.冒泡排序是内循环的相邻两个值做比较修改

  6. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

  7. 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题要求 ...

  8. 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.注意要点:第一个元素 ...

  9. [array] leetCode-27. Remove Element - Easy

    27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...

随机推荐

  1. Jmeter-常用函数之__CSVRead使用

    可参照:http://www.cnblogs.com/liu-ke/p/4324157.html 压测中有些参数是固定范围内的取值,或者成对出现(如登录帐号和密码),以大量用户同时登录网站为例: 1. ...

  2. Android大图片裁剪终极解决方案(下:拍照截图)

    http://blog.csdn.net/floodingfire/article/details/8144617 http://mzh3344258.blog.51cto.com/1823534/8 ...

  3. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  4. 自学安卓练习作品单词APP(1)-安卓的hello word与有道字典防爬虫破解

    1.前言 闲来无聊.手机每天又是都接触的东西.程序什么的最容易接触到.想到有些人说前后端都做就是全栈的说法.哦,你看html5全栈. 要我说多接触一些多有意思.天天写后端.还不是业务层.又不是什么高大 ...

  5. CentOS7利用本地yum源配置NBD

    一:CentOS7.0创建本地YUM源 (物理机:直接将刻录的CentOS7光盘利用光驱插入物理机上) (虚拟机: CD/DVD>>连接本地ISO) 针对物理机192.168.9.112进 ...

  6. 并发之AtomicIntegerFieldUpdater

    基于反射的实用工具,可以对指定类的指定 volatile int 字段进行原子更新.此类用于原子数据结构,该结构中同一节点的几个字段都独立受原子更新控制. 先来看一段代码: package autom ...

  7. c#数据库访问服务(综合数据库操作)

    前面给大家说封装了常用的数据库,并且整理了使用.最近我再次把项目整合了.做成比较完善的服务. 还是重复的说下数据库操作封装. berkeley db数据库,Redis数据库,sqlite数据库. 每个 ...

  8. webpack之理解loader

    我们在写webpack配置文件的时候,应该有注意到经常用到loader这个配置项,那么loader是用来做什么的呢? loader其实是用来将源文件经过转化处理之后再输出新文件. 如果是数组形式的话, ...

  9. 基于jQuery+JSON的省市县 二级 三级 联动效果

    省市区联动下拉效果在WEB中应用非常广泛,尤其在一些会员信息系统.电商网站最为常见.开发者一般使用Ajax实现无刷新下拉联动.本文将讲述,利用jQuery插件,通过读取JSON数据,实现无刷新动态下拉 ...

  10. linux系统基础之---RAID(基于centos7.4 1708)