题目

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. jQuery笔记之 Ajax回调地狱

    本次演示回调地狱: 模拟电影网站,判断用户是否为该网址的vip用户(最高权限为vip) 如果vpi就展示出vip电影,点击相应的电影显示出该电影的详细介绍 ---------------------- ...

  2. 使用selesium和pytesseract识别验证码,达到登录网页目的

    关于验证码问题,大多可以在网上了解到目前有四种解决方案:1.开发注释验证码2.开发开一个“后门”,设置一个万能码,输入万能码则通过3.通过cookies绕过验证码4.图形识别技术 前三种是比较快速也是 ...

  3. Java | 技术应用 | 利用Jsoup处理页面

    根据微信公众号的推文链接地址,对文章内容进行爬取,利用jsoup解析文章源代码,加上结合xpth提取文文章信息, 利用正则表达式读取文章发表时间. Jsoup <!-- jsoup HTML p ...

  4. ulimit资源配置

    基本理解 linux对每个用户能使用的系统资源有一定限制.如果没有限制,在多用户登录,并且都消耗大量资源时,对系统产生复杂的影响.ulimit内建一套参数,来规定一个用户能使用多少资源. [root@ ...

  5. centOS 部署服务器(一)

    接下来我所写的博客仅仅是为了记录我的学习过程,与其他无关. 由于公司换用了亚马逊服务器,用的是它的RDS数据库,所以就没有像以前的项目部署的时候使用mysql,不过要下载安装mysql-proxy,字 ...

  6. spring mvc支持跨域请求

    @WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...

  7. spring-bean(三)

    配置方式:通过工厂方法配置bean,通过FactoryBean配置bean 配置形式:基于注解的方式 组件扫描 泛型依赖注入 静态工厂方法 /** * 静态工厂方法:直接调用某一个类的静态方法就可以返 ...

  8. Google的Java编程风格指南

    作者:Hawstein出处:http://hawstein.com/posts/google-java-style.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Cre ...

  9. ubuntu下安装memcache及memcached

    memcache 和 memcached 有什么区别呢? memcache最早是在2004年2月开发的,而memcached最早是在2009年1月开发的.所以memcache的历史比memcached ...

  10. Docker Hello World容器运行报错的解决办法

    费了好大力气从Docker官网下载了Docker Community Editor的安装镜像,Docker.dmg, 总共将近500MB,双击进行安装: 命令行里使用docker version查看版 ...