//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]. #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(int a[], int n)
{
if (n < 2)
{
return n;
} int p = 0;
int pn = 1;
while(pn < n)
{
if (a[p] == a[pn])
{
pn++;
}
else
{
p++;
a[p] = a[pn];
pn++;
}
}
return p+1;
} // int removeSTL(int a[], int n)
// {
// return distance(a, unique(a, a + n));
// }
}; int main()
{
int a[12] = {1,1,2,2,3,4,4,7,7,7,8,9}; Solution s;
cout<<s.removeDuplicates(a,12)<<endl;
// cout<<s.removeSTL(a,12)<<endl; return 0;
}

Remove duplicates from array的更多相关文章

  1. Remove duplicates from array II

    //Given a sorted array, remove the duplicates in place such that each element appear only // once an ...

  2. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

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

  3. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  4. Remove Duplicates from Sorted Array II

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

  5. Remove Duplicates from Sorted Array II [LeetCode]

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

  6. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  7. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

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

  9. 26. Remove Duplicates from Sorted Array

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

随机推荐

  1. VIM编辑器和VI编辑器的区别

    vi 和vim 的区别 写在前面:这个两个"东西"着实让我烦恼一阵子,但是自己一直没当回事,但是遇到了好几次再决定彻底把他们搞的明白,一下是我通过查找资料了解到的关于这两个编辑器的 ...

  2. codeforces587a//Duff and Weight Lifting// Codeforces Round #326 (Div. 1)

    题意:一数列an,如果存在一个k,有2^(ai)+2^(aj)+......=2^k成立,那么一次能拿走ai,aj这些全部.问最少拿的次数. 太简单. 乱码 //#pragma comment(lin ...

  3. ssd制作数据和训练

    1.在/data/VOCdevkit下建立自己的数据集名称如MyDataSet,在MyDataSet目录下需包含Annotations.ImageSets.JPEGImages三个文件夹: 2.Ima ...

  4. vi常用快捷键

    vi常用快捷键 1)移动光标 h :光标左移一个字符k :光标上移一个字符j :光标下移一个字符l :光标右移一个字符 0 :光标移至行首$ :光标移至行尾 H :光标移至屏幕首行M :光标移至屏幕中 ...

  5. CF1117E Decypher the String

    如果我们能询问一个排列的话,我们就可以得到这个置换,然后反向求解. 但现在字符集只有26. 考虑26^3>1e5. 用一个三维坐标去映射到一个一维整数,然后就可以构造排列了. #include& ...

  6. php 字符编码转换

    <?php/** * Created by PhpStorm. * User: yxp * Date: 2016/11/8 * Time: 16:47 */ /** * 将非GBK字符集的编码转 ...

  7. HTTP及RFC解析。

    HTTP协议描述的是发送方与接收方的通信协议,通过两方的自觉遵守而存在,当然有不少的浏览器并没有百分百遵守这份协议.HTTP是运行于应用层的协议,基于TCP协议而运作.基本上是客户/服务器对答模式,其 ...

  8. 【LeetCode】跳跃游戏

    给定一组非负整数,初始时处于数组的第一位下标 0 的位置,数组的每个元素代表那个位置可以跳跃的最大长度.判断你是否能够到达数组的最后一位下标. e.g. A = [2, 3, 1, 1, 4],返回 ...

  9. java旅程(一) 配置环境

    (一)安装java JDK   下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  10. MySQL查询性能调优化

    一.索引的概念 索引:类似于字典的目录,设置索引可以 加速数据查找,对数据进行约束: 二.索引类型: 主键索引:保证数据唯一性,不能重复+不能为空 普通索引:加速数据查找 唯一索引:加速查找+不能重复 ...