题目

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 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.

分析

这么一个简单的题目,竟然3次才AC。太失败了!

总结,就是只顾得求出不重复元素的个数,却忽略了输入参数是引用,必须同时使得数组中存储为不重复元素。也就是求取数目的同时必须更新数组中元素不能重复。

AC代码

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= 1)
return nums.size(); int i = 0, j = 1;
while (j < nums.size())
{
if (nums[i] == nums[j])
{
++j;
}
else
{
nums[++i] = nums[j++];
}
}
return i + 1;
}
};

GitHub测试程序源码

LeetCode(26) Remove Duplicates from Sorted Array的更多相关文章

  1. LeetCode(28)-Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  2. LeetCode(80)Remove Duplicates from Sorted Array II

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

  3. 【LeetCode算法-26】Remove Duplicates from Sorted Array

    LeetCode第26题 Given a sorted array nums, remove the duplicates in-place such that each element appear ...

  4. leetcode第26题--Remove Duplicates from Sorted Array

    problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...

  5. LeetCode(82)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  6. LeetCode(83)Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...

  7. 【26】Remove Duplicates from Sorted Array

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

  8. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  9. LeetCode(33)Search in Rotated Sorted Array

    题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

随机推荐

  1. 我人生中的第一场Java面试

    1.说起我的第一次Java面试,我不禁回想起我大学时参加校园招聘的那段日子,那时候我还是本科生,由于不是科班出身,只学过一点点Java皮毛,所以那时候对于找Java工作并没有什么概念,只是以为上过Ja ...

  2. JMeter配置JDBC测试SQL Server/MySQL

    一.配置SQL Server 1.下载sql驱动,将sqljdbc4.jar放到JMeter安装目录/lib下. 2.启动JMeter,右键添加->配置文件->JDBC Connectio ...

  3. css3 background-size属性--ie兼容

    css3 background-size属性--ie兼容 safari浏览器对background-size 属性显示原始大小 background-size: cover not working o ...

  4. MySQL执行带out的存储过程

    CALL `sp_sys_get_code`(3,'sys_customer',@code); SELECT @code

  5. 【C#】什么时候使用virtual什么时候使用abstract,(另附override/new区别)

    一.C#中virtual与abstract的区别(引用“姓吕名布字子乔”的文章) C#的virtual & abstract经常让人混淆,这两个限定词都是为了让子类进行重新定义,覆盖父类的定义 ...

  6. 洛谷P2774 方格取数问题(最小割)

    题意 $n \times m$的矩阵,不能取相邻的元素,问最大能取多少 Sol 首先补集转化一下:最大权值 = sum - 使图不连通的最小权值 进行黑白染色 从S向黑点连权值为点权的边 从白点向T连 ...

  7. ES6学习笔记(10)----Set和Map数据结构

    参考书<ECMAScript 6入门>http://es6.ruanyifeng.com/ Set和Map数据结构 1.Set  基本用法    Set是一种新的数据结构,它的成员都是唯一 ...

  8. java8的lambda表达式,将List<DTO> 转为 List<DO>

    将List<PhoneDTO>转为List<PhoneDO>,通过java8的lambda表达式来操作,比传统的for循环精简很多: /** * List<PhoneDT ...

  9. Android 友盟和微信的包冲突:Multiple dex files define Lcom/tencent/a/a/a/a/a;

    最近App中有个需求是添加微信支付,就在微信技术官网 http://open.weixin.qq.com,查看一下文档,然后下载SDk,Demo.把SDK集成进项目. 照着微信的文档,把jar包和进来 ...

  10. Cognos添加关联字段

    (这是另一个表)