LeetCode全集请参考:LeetCode Github 大全

题目

26. Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means a modification to the input array will be known to the caller as well.

Internally you can think of this:

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {print(nums[i]);
}

Example 1:

Input: nums = [1,1,2]
Output: 2, nums = [1,2]
Explanation: 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 returned length.

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]
Explanation: Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.

Constraints:

0 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums is sorted in ascending order.

用两个指针解法

  1. 判断边界,如果数组为空则返回0;
  2. 记录上一个数字pre,和目前没有重复的位置index;
  3. 遍历数组一直到找出跟上个数字不等,则把当前数字赋值给下个index,并用当前数字替换pre;
class Solution {public int removeDuplicates(int[] nums) {// check edgeif (nums == null || nums.length == 0) {return 0;}int pre = nums[0];int index = 1;for (int k = 1; k < nums.length; k++) {if (nums[k] != pre) {nums[index] = nums[k];index++;pre = nums[k];}}return index;}
}

算法:从有序数组中移除重复的数据26. Remove Duplicates from Sorted Array相关推荐

  1. code第一部分数组:从有序数组中移除重复的数据

    code第一部分数组:从有序数组中移除重复的数据 第二题 从有序数组中移除重复的数据,但是可以保留2个重复的数. For example, Given sorted array A = [1,1,1, ...

  2. C#LeetCode刷题之#26-删除排序数组中的重复项(Remove Duplicates from Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3622 访问. 给定一个排序数组,你需要在原地删除重复出现的元素, ...

  3. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

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

  4. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  5. LeetCode算法 删除有序数组中的重复项 删除有序数组中的重复项|| C++

    目录 题目 删除有序数组中的重复项 参考答案 题目 删除有序数组中的重复项|| 参考答案 题目 删除有序数组中的重复项 给你一个有序数组 nums ,请你原地删除重复出现的元素,使每个元素只出现一次 ...

  6. LeetCode.961-2N数组中N次重复的元素(N-Repeated Element in Size 2N Array)

    这是悦乐书的第365次更新,第393篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第227题(顺位题号是961).在大小为2N的数组A中,存在N+1个唯一元素,并且这些元 ...

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

    题目: 删除排序数组中的重复数字 II 跟进"删除重复数字": 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A ...

  8. 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II

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

  9. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  10. 删除重复值(2以上)Remove Duplicates from Sorted Array II

    2019独角兽企业重金招聘Python工程师标准>>> 问题: Follow up for "Remove Duplicates": What if duplic ...

最新文章

  1. python里混淆矩阵 左下角为漏报,右上角为误报
  2. javaWeb中的Model1和Model2
  3. zookeeper分布式锁的原理
  4. jquery(ajax)+ashx简单开发框架(原创)
  5. [八省联考2018]劈配 (匈牙利)
  6. 用C/C++开发《Photoshop》图像处理软件
  7. 手机MODEM 开发(24)---93 modem如何打开GSM AMR-WB
  8. 为什么黑客都用python-为什么黑客都用python Python在各领域都有着广
  9. iOS-绘制UIView之drawCGRect
  10. 一文讲清楚ojdbc、Oracle和JDK之间的兼容性关系
  11. JavaScript 设计模式之模板方法模式
  12. 解决Go服务GC时间过长
  13. android 自动跳转市场,js判断设备,跳转app应用、android市场或者AppStore
  14. 2020全球搜索引擎市场份额排行榜
  15. 阿里轻量应用服务器搭建ftp服务器
  16. 仿抖音写上下滑动切换视频
  17. 小汽车的位置(二维坐标运算)
  18. 干货 | 手把手教你搭建一套OpenStack云平台
  19. vue 数字动画递增_数字滚动动画效果 vue组件化
  20. 抖音这样拍摄短视频,能让你轻松上热门涨粉丨国仁网络资讯

热门文章

  1. 计算机应用技术在医院的应用,计算机管理技术在医院的应用
  2. python3使用staf问题_python3 使用时遇到的问题
  3. matlab模式识别大作业_史上最萌最认真的机器学习/深度学习/模式识别入门指导手册(二)...
  4. fastcopy会损坏硬盘_绚丽多彩 卓有不凡 三星移动固态硬盘T5金属红新品评测
  5. JavaScript内置对象之Array对象总结(附实例)
  6. HttpApplication 类
  7. Slowquery图形化显示MySQL慢日志工具
  8. How is NGINX Unit different from Apache?
  9. Java 实验五 王奕开
  10. linux 文件系统的简单操作