题目:

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. 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  2. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  3. 六、C++离散傅里叶逆变换

    C++离散傅里叶逆变换 一.序言: 该教程承接上文的离散傅里叶变换,用于进行离散傅里叶逆变换. 二.设计目标 对复数数组进行离散傅里叶逆变换,并生成可供使用的图像类. 三.详细步骤 输入:经傅里叶变换 ...

  4. Oracle数据库基础--建表语法+操作

    语法 1.建表 create table 表名( 列名 数据类型, …… ); 2.删除表:drop table 表名; 3.添加列:alter table 表名 add(列名 数据类型); 4.修改 ...

  5. 真实场景中WebRTC 用到的服务 STUN, TURN 和 signaling

    FQ收录转自:WebRTC in the real world: STUN, TURN and signaling WebRTC enables peer to peer communication. ...

  6. IOS Prefix.pch程序常见文件 的作用

    #import <Availability.h> #ifndef __IPHONE_5_0 #warning "This project uses features only a ...

  7. Android(java)学习笔记83:各种边距设置

    1. 各种边距设置: (1)android:layout_paddingLeft 内边距,对谁用,指的是谁的内部内容边距 (2)android:layout_marginLeft 外边距,对谁用,指的 ...

  8. pycharm 安装插件 支持markdown

    github项目中的README文件通常是md格式的,但是pycharm默认是不支持的,需要安装插件 进入settings中搜索plugins,然后在plugins中搜索markdown suppor ...

  9. Python实现注册和登录

    一.注册账号需要实现的功能 1.输入:用户名,密码,密码确认 2.限制1:输入的账号和密码不能为空 3.限制2:两次输入密码必须一致 4.限制3:用户名不能重复 5.限制4:错误次数为4次 6.用字典 ...

  10. cuda中当元素个数超过线程个数时的处理案例

    项目打包下载 当向量元素超过线程个数时的情况 向量元素个数为(33 * 1024)/(128 * 128)=2.x倍 /* * Copyright 1993-2010 NVIDIA Corporati ...