题目:

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

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

代码:

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

Tips:

O(n)时间复杂度 O(1)空间复杂度

常用数组去重技巧 记住即可

=============================

第二遍刷的时候,第二次才AC。原因是++prev第一次写成了prev++,细节要注意。

class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if ( nums.size()== ) return ;
int prev = ;
for ( int i=; i<nums.size(); ++i )
{
if ( nums[i]!=nums[prev] )
{
nums[++prev] = nums[i];
}
}
return prev+;
}
};

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

  1. leetcode 【 Remove Duplicates from Sorted Array 】python 实现

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

  2. 【Remove Duplicates from Sorted List 】cpp

    题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...

  3. 【Remove Duplicates from Sorted Array II】cpp

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

  4. leetcode 【 Remove Duplicates from Sorted Array II 】python 实现

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

  5. 【Search In Rotated Sorted Array】cpp

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

  6. leetcode 【 Remove Duplicates from Sorted List 】 python 实现

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

  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】#80. Remove Duplicates from Sorted Array II

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

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

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

随机推荐

  1. C#Udp组播

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...

  2. uLua学习之使用协程(终)

    前言 今天是本系列的第六篇文章,也是最后一篇,我们来看看uLua中如何来实现协程吧.首先,让我们明确协程的概念.在百度百科上的是这样说的,协程更适合于用来实现彼此熟悉的程序组件,如合作式多任务,迭代器 ...

  3. linux 命令——13 less(转)

    less 工 具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前 ...

  4. iOS中的崩溃类型

    http://blog.csdn.net/womendeaiwoming/article/details/44243571 OS中的崩溃类型 在这里了解一下XCode用来表示各种崩溃类型的术语,补充一 ...

  5. 从用户访问网站流程开始,细说web网络基础

    1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...

  6. 第七章 动态创建HTML内容

    javascript也可以改变网页的结构和内容 document.write()方法 可以方便快捷地把字符串插入到文档里 document.write("<strong>hell ...

  7. 【转】mongoDB 学习笔记纯干货(mongoose、增删改查、聚合、索引、连接、备份与恢复、监控等等)

    mongoDB 学习笔记纯干货(mongoose.增删改查.聚合.索引.连接.备份与恢复.监控等等) http://www.cnblogs.com/bxm0927/p/7159556.html

  8. AJAX进行分页

    新建数据集:PagingDataSet.xsd SELECT * from ( select id, areaID, area, father,Row_Number() over (order by ...

  9. 微服务SpringCloud+Docker入门到高级实战(教程详情)

    第一章 课程介绍和学习路线 1.微服务架构SpringCloud课程介绍 简介:课程介绍和课程大纲讲解,讲课风格和重点内容理解技巧 2.技术选型和学后水平 简介:课程所需基础和技术选型讲解,学完课程可 ...

  10. JavaScript获取时间戳与时间戳转化

    第一种方法(精确到秒): var timestamp1 = Date.parse( new Date()); 第二种方法(精确到毫秒): var timestamp2 = ( new Date()). ...