Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

解题思路1:

数组中相似的元素,不能像链表一样删除重构。可以使用两个index,一个用来遍历原数组,另一个用来标记新构造数组的尾部/长度。

所谓新构造数组,只是在原数组的空间上做填充,不会使用新的空间。

代码:

 class Solution {
public:
int removeDuplicates(int A[], int n) {
if (n==)
return n; int newArrayLen = ; for (int i=; i<n-; i++) {
if (A[i] != A[i+]) {
A[newArrayLen++] = A[i+];
}
} return newArrayLen;
}
};

附录-

【Leetcode】【Easy】Remove Duplicates from Sorted Array的更多相关文章

  1. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  2. LeetCode Array Easy 26.Remove Duplicates from Sorted Array 解答及疑惑

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

  3. LeetCode Linked List Easy 83. Remove Duplicates from Sorted List

    Description Given a sorted linked list, delete all duplicates such that each element appear only onc ...

  4. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  6. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  7. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 【LeetCode】080. Remove Duplicates from Sorted Array II

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

  9. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  10. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

随机推荐

  1. 【算法笔记】B1031 查验身份证

    1031 查验身份证 (15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2 ...

  2. poj2420 A Star not a Tree? 找费马点 模拟退火

    题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...

  3. vue修改组件样式

    .el-date-editor /deep/ input{ padding-left:30px; } 改变引入的组件里面元素的样式: 1.去掉css内的scoped,但是这样会污染全局 2.加上/de ...

  4. Eclipse for PHP Developers 配置记录

    [原文发表在 http://osworld.sinaapp.com/post/18.html] 图都粘贴不了,直接看上面的原文吧~~~ 我比较懒,还是比较依赖IDE环境做开发的.所以为了学PHP开发, ...

  5. sqlmap命令小结

    --technique 这个参数可以指定sqlmap使用的探测技术,默认情况下会测试所有的方式. 支持的探测方式如下: B: Boolean-based blind SQL injection(布尔型 ...

  6. Shiro登录的故事

    从前,有一个subject,他有一个UsernamePasswordToken的实例,也就是token: 他准备登录,于是调用subject.login(AuthenticationToken tok ...

  7. PIE SDK波段运算

    1.算法功能简介 波段运算(Band Math)工具能够方便的执行图像中的各个波段的加减乘除.三角函数.指数.对数等数学函数计算,也可以使用IDL编写的函数. 由于每个用户都有独特的需求,利用此工具用 ...

  8. PIE SDK小波变换

    1.算法功能简介 小波变换是一种信号的时间——尺度分析方法,具有多分辨率分析的特点,而且在时频两域都具有表征信号局部特征的能力,是一种窗口大小固定不变但其形状可变,时间窗和频率窗都可变的时频局部化分析 ...

  9. Python max 和 min高级用法

    zip max 比较一个字典,是按key比较 如果想比较字典的value max比较只能是相同类型,比如字符串和数字就不能比较会报错 这种会报错

  10. temp脚本

    !/bin/bash source ${HOME_DIR}/script/ideploy_dm.inc source ${HOME_DIR}/script/comm_lib home_dir=$(cd ...