题目:

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. centos7.4 安装后的基本设置

    centos7.4 安装后的基本设置 设置主机名称 设置IP地址,网关 修改网卡名称 内核优化 系统安全设置 防火墙设置 ssh设置 同步系统时间 安装基础软件包 软件配置 设置主机名称 hostna ...

  2. python3基础01(常见语法基础汇总)

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 换行\n 续行\ s[:i] + s[i:] 等于 s#转义 \e 空 \000 八进制 \oyy 十六进制 ...

  3. centos开机启动自定义脚本

    有些时候我们需要在服务器里设置一个脚本,让他一开机就自己启动.方法如下: cd /etc/init.d vi youshell.sh #将youshell.sh修改为你自己的脚本名 编写自己的脚本后保 ...

  4. cms-帖子管理

    mapper: <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC & ...

  5. GetIPAddress——获得本地IP地址信息

    1.gethostname()需要初始化套接字库 加载#pragma comment(lib,"ws2_32.lib"),和WSAStartup(MAKEWORD(2,2),&am ...

  6. HDU4302 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4302 , 可以用线段树,也可以STL中的map,multiset,优先队列中的任何一个解决(可我只会线 ...

  7. 配置Maven镜像与本地缓存

    IntelliJ IDEA 安装后自带Maven,也可以使用自己安装的Maven. 配置阿里镜像与本地仓库文件夹 找到Maven的安装目录 打开settings.xml配置文件   修改mirrors ...

  8. mongostat查看mongodb运行状态使用命令介绍

    mongostat是mongodb自带的一个用来查看mongodb运行状态的工具 使用说明 mongostat -h   字段说明 启用后的状况是这样的 insert query update del ...

  9. Hybrid App开发之css样式使用

    前言: 前面学习了html,今天学习一下css的基本使用,看下html与css之间是如何结合来编写前端网页的. CSS 是什么? CSS 是 Cascading Style Sheets(级联样式表) ...

  10. Ubuntu安装mysql和简单使用

    一.安装mysql sudo apt-get install mysql-server sudo apt-get isntall mysql-client sudo apt-get install l ...