题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次。返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1)

举例:

Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

解题思路:

1. 由于数组本身是有序的,且题目要求使用原地排序,因此结果也肯定是有序的(出去数组末尾的哪些被删除的数据) 1112333

2. 采用两个标志位,一个在当前位begin,另一个向后遍历(从begin的下一位开始),直到遇到与当前位不一样的数字,在此之间的所有相同数字,统一置为nums[0],即将与数组中所有第一次出现的数字相同的所有数组都置为0;此时数组变成1112311

3. 依然采用两个标志位,start表示除了nums[0]之外的下一个与nums[0]相等的数的位,index表示第一个与nums[0]不相等的数的位,交换彼此;一次交换的结果变为1211311,两次交换的结果为1231111

4.  每交换一次,表示有一个不被删除的元素,再加上第一个元素,结果为count + 1;

代码如下:

 public class Solution {
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length < 1)
return 0;
int begin = 0;
int count = 0;
      // 将与数组中所有第一次出现的数字相同的所有数字都置为nums[0]
for(int i = 1; i < nums.length; i++){
if(nums[i] == nums[begin]){
nums[i] = nums[0];
continue;
}
begin = i; // 下一个第一次出现的数字
}
int index = 1;
int start = 1;
while(index < nums.length){
if(nums[index] == nums[0]){ // 找到与nums[0]不相同的那个位
index ++;
continue;
}
exchange(nums, start, index); // 交换
start ++;
count ++;
index ++;
}
return count + 1; // 最终交换的次数 + 1
}
public void exchange(int[] nums, int index1, int index2){
if(index1 == index2)
return;
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
}

Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)的更多相关文章

  1. lintcode :Remove Duplicates from Sorted Array 删除排序数组中的重复数字

    题目: 删除排序数组中的重复数字 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成.  样例 ...

  2. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  3. [LeetCode]26. Remove Duplicates from Sorted Array删除排序数组中的重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  4. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  5. 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项

    给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...

  6. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  8. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  9. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

随机推荐

  1. telegraf1.8+influxdb1.6+grafana5.2 环境搭建 结合JMeter3.2

    telegraf1.8+influxdb1.6+grafana5.2 环境搭建 结合JMeter3.2 参考地址:https://blog.csdn.net/laisinanvictor/articl ...

  2. js进阶之闭包

    1.首先说下什么是闭包,闭:封闭,包:就像我们的包包~:虽然我在扯淡但是也有其道理咯,闭包就有点像java里面的封装一样,包属性和方法都封装到对象内部,在外部通过共有的get.set方法获取或者设置其 ...

  3. 不写画面的网页程序设计,Web API、Web Service、WCF Service

    客户有一个系统,经常要连上我方,查询数据 以前的作法是给对方一个账号,让他可以连上我们的DB来查询. 所以,早期的同仁,真的给他们DB链接字符串 客户的Windows程序.网站就真的靠这么危险的方式, ...

  4. Context 使用不当造成内存泄露

    问题: Activity中的context被传递给了一个生命周期长过activity的对象(通常为静态单实例变量),导致activity不能正常被销毁. 示例:Activity 调用 ChatMgr ...

  5. WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread

    [转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...

  6. CPP-网络/通信:gsoap 的教程和使用

    1.1.1     gSOAP 1.1.1 .1      简介 gSOAP 编译工具提供了一个 SOAP/XML 关于 C/C++ 语言的实现,从而让 C/C++ 语言研发 web 服务或客户端程式 ...

  7. 【数学 BSGS】bzoj2242: [SDOI2011]计算器

    数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...

  8. 【Linux】开放指定端口设置

    这里以开放tomcat的8080端口为例 1.开放Linux的8080端口 vi /etc/sysconfig/iptables 进入编辑页面,在指定位置新增以下配置 -A INPUT -m stat ...

  9. MYSQL导入数据:Table XXX doesn't exist的解决

    数据表为Innodb引擎 data文件夹中存在数据表的frm文件,但在phpmyadmin中看不到这些表,于是采用导入sql文件的方式进行恢复 1.直接导入原数据表的sql文件,原frm文件不删除 出 ...

  10. 【php】【趣味代码】对象引用的比较

    <?php $a = new stdClass(); $a->name = 'flint'; $b = $a ; $b->sex = 'man'; saveObject($b); f ...