RemoveDuplicatesFromSortedArrayI:

问题描述:给定一个有序数组,去掉其中重复的元素。并返回新数组的长度。不能使用新的空间。

[1,1,2,3] -> [1,2,3] 3

算法思路:用一个数字记录新数组的最后一个元素的位置

 public class RemoveDuplicatesFromSortedArray {

     public int removeDuplicates(int[] nums)
{
if(nums.length == 0)
{
return 0;
}
int key = nums[0];
int count = 0;//记录新数组最后一个元素的位置,即新数组长度
for(int i = 0; i < nums.length; i ++)
{
if(nums[i]!=key)
{
nums[count++] = key;
key = nums[i];
}
}
nums[count++] = key;
return count;
}
}

与之类似的就是移除数组里某个元素。

 public int removeElement(int[] nums, int val) {
if(nums.length == 0)
{
return 0;
}
int count = 0;
for(int i = 0; i < nums.length; i ++)
{
if(nums[i]!=val)
{
nums[count++] = nums[i];
}
}
return count;
}

RemoveDuplicatesFromSortedArrayII:

问题描述:可以重复一次。

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

算法分析:这种题目,要巧妙利用数组的下标,和上一题解法相似,只不过,循环里的判断条件变了。

//[1,1,1,2,2,3] -> [1,1,2,2,3] 5 允许重复一次
public class RemoveDuplicatesfromSortedArrayII
{
public int removeDuplicates(int[] nums)
{
if(nums.length == 0 || nums.length == 1)
{
return nums.length;
}
int count = 1, temp = nums[1];
for(int i = 2; i < nums.length; i ++)
{
if(nums[i] != nums[i - 2])
{
nums[count++] = temp;
temp = nums[i];
}
}
nums[count++] = temp;
return count;
}
}

RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素的更多相关文章

  1. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. 每日一道 LeetCode (8):删除排序数组中的重复项和移除元素

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  3. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  7. 82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  8. 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  9. 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串

    最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...

  10. LeetCode(26): 删除排序数组中的重复项

    Easy! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...

随机推荐

  1. mysql如何判断一个字符串是否包含另外一个字符串?

    转自:http://blog.csdn.net/hechurui/article/details/49278493 判断子字符串在父字符串当中的索引: SELECT LOCATE("b&qu ...

  2. bootstrap-table固定表头固定列

    1.引入 bootstrap依赖于jquery bootstrap-table依赖于bootstrap,所以都需要引入 2. bootstrap-table有两种方式,html.js <tabl ...

  3. nodemailer发送邮件各个服务器接口

    来自:https://github.com/nodemailer/nodemailer-wellknown/blob/master/services.json#L125 {   "1und1 ...

  4. 记录--常用的linux命令

    mysql event /*查询event是否开启(查询结果Off为关闭 On为开启)*/ show variables like '%sche%'; /*开启/关闭命令(1开启--0关闭)*/ se ...

  5. 记录--jquery 获取父级、子级、兄弟元素 + 实例

    需求如下: 三条数据,需点击其中一条数据在其下面展示与此数据关联的图片.主要功能可能是在点击的数据下增加显示行 思路: 把需要点击增加的数据先隐藏.点击后再将其显示出来. 知识点: jQuery.pa ...

  6. Spoken English Practice(Look, That cute guy is checking me out. come on, give me a break, he's just looking around.)

    绿色:连读:                  红色:略读:               蓝色:浊化:               橙色:弱读     下划线_为浊化 口语蜕变(2017/7/6) 英 ...

  7. Storm-源码分析-acker (backtype.storm.daemon.acker)

    backtype.storm.daemon.acker 设计的巧妙在于, 不用分别记录和track, stream过程中所有的tuple, 而只需要track root tuple, 而所有中间过程都 ...

  8. Linux基础命令(四)

    作业一:1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 [root@bogon ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 byte ...

  9. django博客项目11

    .....................

  10. 对数值数据的格式化处理(保留小数点后N位)

    项目中有时会遇到对数值部分进行保留操作,列如保留小数点后2位,所有的数据都按这种格式处理, //保留小数点后2位,都按这种格式处理,没有补0 DecimalFormat df = new Decima ...