题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次。返回新数组的长度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. Promise 对象与Generator 函数

    异步编程的方法,大概有下面四种: 回调函数 事件监听 发布/订阅 Promise 对象 传统的编程语言,早有异步编程的解决方案(其实是多任务的解决方案).其中有一种叫做"协程"(c ...

  2. Android中的GreenDao框架修改数据库的存储路径

    目前android中比较热门的数据库框架有greenDAO.OrmLite.AndrORM,其中我比较喜欢用GreenDao,其运行效率最高,内存消耗最少,性能最佳.具体怎么使用GreenDao,网上 ...

  3. 命令方式重新签名apk

    1.(每个指令之间要有一个空格) 注:拿到一个apk后,首先删除META-INF. 1.如果你的电脑装的是jdk1.6,就用下面的命令: 打开命令符,首先直接输入: Jarsigner -keysto ...

  4. [SecureCRT]通过SFTP方式上传本地文件到服务器

    1.在本地建一个文件夹,如:d:\My Documents,在此目录下,放入我们需要上传的文件,如:nmon_linux_x86_64 2.然后打开我们的SecureCRT工具,一次选择Options ...

  5. python基础教程总结4—基本语句

    一.print 和 import 的更多信息 print 打印多个表达式也是可行的,只要将它们用逗号隔开就好: >>> print('Age:' , 42) Age: 42 可以看到 ...

  6. UVA 11992 Fast Matrix Operations (降维)

    题意:对一个矩阵进行子矩阵操作. 元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作. 一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测 ...

  7. 【转】iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...

  8. 头文件种的ifndef/define/endif 是干什么用的

    头文件种的ifndef/define/endif 是干什么用的? 答:防止头文件被重复包含.

  9. 在Linux下安装redis

    http://www.cnblogs.com/xiaohongxin/p/6854095.html 追加: 通过配置文件启动最好先./redis.cli shutdown ,再到当前目录在./redi ...

  10. Nginx超时配置

    Nginx超时配置 1.client_header_timeout 语法client_header_timeout time 默认值60s 上下文http server 说明 指定等待client发送 ...