//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. ssh服务及安全配置

    1.清空防火墙 关闭 setenforcesetenforce   2 getenforce 3 setenforce 0 4 iptables -F 5 systemctl stop firewal ...

  2. tchart5

    https://blog.csdn.net/wuyuanjingni/article/details/8585810

  3. BGP - 1,基本概念

    1,BGP知识点 a)AS号:私有(64512-65535),公有(0-64511). b)什么时候使用BGP:有数据穿越本AS前往其他AS:本AS有多条到其他AS的连接:必须要做策略.   c)BG ...

  4. Div不用float布局

    CSS代码 .wrapper1_4 { width: 100%; /* 也可以固定宽度 */ height: 26px; } .wrapper1_4 > .left { display: inl ...

  5. 0.1.2 max_element和min_element的用法

    找到的位置都是第一个最大(小)的元素,即存在多个相同大小的元素的时候找到的是第一个. 返回的是指针(元素地址). printf("%d\n",*max_element(a,a+n) ...

  6. 【洛谷p1313】计算系数

    (%%%hmr) 计算系数[传送门] 算法呀那个标签: (越来越懒得写辽)(所以今天打算好好写一写) 首先(ax+by)k的计算需要用到二项式定理: 对于(x+y)k,有第r+1项的系数为:Tr+1= ...

  7. CoordinatorLayout、AppBarLayout、CollapsingToolbarLayout的用法,让Toolbar与系统栏融为一体

    CoordinatorLayout其实是加强版的FrameLayout布局,可以监听期所有子控件的各种事件,由Design Support库提供的,能体现Material Design 的魔力.能解决 ...

  8. HDU 5710 Digit Sum

    Let S(N)S(N) be digit-sum of NN, i.e S(109)=10,S(6)=6S(109)=10,S(6)=6. If two positive integers a,ba ...

  9. 『计算机视觉』Mask-RCNN_锚框生成

    Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...

  10. vue methods computed watch区别

    一.methods和computed computed是计算属性,methods是方法. html: <p>Reversed message: "{{ reversedMessa ...