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. 巨蟒python全栈开发数据库攻略4:多表操作&Navicat&pymysql

    1.多表查询 2.连表补充 3.boss工具=>Navicat 4.索引加速寻找工具=>everything 5.pymysql 6.pymysql初识 7.pymysql的各个方法

  2. 第二课——解析mysqldump命令和mysqlbinlog命令+innodb和Myisam存储引擎简介

    环境说明 mysql版本:Percona-Server-5.6.30 IP:10.7.15.167 端口:3306 安装目录:/httx/run/mysql 数据目录:/httx/run/mysql/ ...

  3. vbs 修改Administrator帐号密码

    Dim WshShell, oExec Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject ...

  4. office 2010 自动连接网络打印机的问题(保存或者打开极慢) 解决方法

    将默认打印机设为本地打印机或 Microsoft XPS Document Writer

  5. js中的typeof name

    js中的name 使用typeof name得到  string.. 因为name是全局变量,可以在任意浏览器中使用 . cosole.dir(window)查看.. console.log(type ...

  6. Storm-源码分析汇总

    Storm Features Storm 简介 Storm Topology的并发度 Storm - Guaranteeing message processing Storm - Transacti ...

  7. javaweb项目中嵌入webservice--axis2

    由于最近项目中需要搭建webservice服务端,由于原项目是javaweb项目,所以需要整合.之前用cxf试了,启动老是报错,maven依赖冲突.后来索性换成axis2 百度了一圈,下面这个博客 h ...

  8. shell正则式解析身份证和手机号

    cat test2.html | sed -e 's/\(^\|[^0-9]\)\(13[0-9][0-9]\{8\}\|14[579][0-9]\{8\}\|15[0-3,5-9][0-9]\{8\ ...

  9. unity 里调试native code

    因项目需要,需要调试dll工程代码. 把生成的debug dll和pdb拷贝进unity的plugins工程,遇到 断点无法进入,修改下调试信息格式,OK.

  10. 000 初步使用Kotlin开发Android应用

    Kotlin是Jetbrians公司开发的一款编程语言,基于jvm兼容Java. 要求 IDE:IDEA或者Android Studio(简称studio)对Kotlin语言有所了解,官方文档:htt ...