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

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. 用HTML编写迪士尼乐园页面

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head lang="e ...

  2. 【Linux资源管理】一款优秀的linux监控工具——nmon

    (一)nmon工具概述 nmon是以一个用来做linux服务器监控的工具,通过nmon,可以实现对以下参数的监控: --CPU使用率 --内存.交换空间使用率 --网络使用情况 --磁盘I/O,读写速 ...

  3. ES5 实现 ES6 的 class以及extends

    ts中是这样的 class Greeter { greeting:string; constructor(message:string){ this.greeting = message; } gre ...

  4. 如何将js字符串变成首字母大写其余小写

    有时候会接收到一些大小写不规则的字符串,如"JAMES"."alice"."Amy"等,如何将他们统一的变成首字母大写其余小写的形式呢? 思 ...

  5. Sass变量及嵌套

    1. 变量:SASS允许使用变量,所有变量以$开头. 变量声明:$highlight-color: #000; 注意:变量可以在css规则块定义之外存在.如下例子: $nav-color: #F90; ...

  6. JSON API免费接口 各种提供JSON格式数据返回服务网站的API接口

    这里为大家搜集了一些能够返回JSON格式的服务接口.部分需要用JSONP调用. 电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID& ...

  7. ziplist之详细分析

    压缩列表ziplist ziplist是一种连续,无序的数据结构.压缩列表是 Redis 为了节约内存而开发的, 由一系列特殊编码的连续内存块组成的顺序型(sequential)数据结构. 组成 属性 ...

  8. 在线预览word,excel文档

    Google Doc 示例:https://jsfiddle.net/7xr419yb/ Microsoft Office 示例:https://jsfiddle.net/gcuzq343/

  9. Java客户端访问HBase集群解决方案(优化)

    测试环境:Idea+Windows10 准备工作: <1>.打开本地 C:\Windows\System32\drivers\etc(系统默认)下名为hosts的系统文件,如果提示当前用户 ...

  10. shell中的死记硬背

    一.shell的引号们 1."" -> 双引号(不保留完整内容,比如遇到$, 反引号, \ 等就会执行相应的shell) echo "Today is `date` ...