题目

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. TC学习总结

    带宽管理: TC中规定描述带宽: mbps = 1024 kbps = 1024 * 1024 bps => byte/s mbit = 1024 kbit => kilo bit/s m ...

  2. scanf()和scanf_s()

    在最初的C语言中,原版的输入就是scanf("<格式化字符串>",<地址表>) ANSI C中没有scanf_s(),只有scanf(),scanf()在读 ...

  3. css width

    转载:http://blog.csdn.net/dddddz/article/details/8631655

  4. python之类的相关名词-继承-

    继承:父类有的功能,子类继承后也都有 继承是直接把父类方法写入子类的object里 如果定义的类有很多重复的功能,可以把重复的类定义成父类 静态方法:不需要实例化就可以调用,不可以调用类里面的变量和方 ...

  5. flask跨域问题

    在Flask开发RESTful后端时,前端请求会遇到跨域的问题.下面是解决方法: 使用 flask-cors库可以很容易的解决   1 pip install flask-cors 两种方法,一个是全 ...

  6. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  7. [转]如何使用MFC和类型库创建自动化项目

    本文转自:http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html 摘要 本文详细介绍了如何自动化像Microsoft Offi ...

  8. Firefox离线安装扩展教程

    Firefox离线安装扩展教程 解决问题博文:解决stackoverflow打开慢不能注册登录 应网友求助在上传了需要的扩展资源后,顺便写个离线安装方法,其实百度也行,这不写下来后为需求者省事.(*^ ...

  9. XDroidMvp 轻量级的Android MVP快速开发框架

    XDroidMvp是XDroidAndroid快速开发框架的MVP版本,其使用方式类似于XDroid,大部分源码也来自XDroid. XDroidMvp主要会有这些特性: 无需写Contract! 无 ...

  10. OCP 11g 第二章练习

    练习 2-1 在Windows计算机上安装SQL Developer 在本练习中,将在Windows计算机上安装SQL Developer 1. 从以下URL下载当前SQL Developer版本: ...