//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. php如何控制客户端生成缓存

    php如何控制客户端生成缓存 一.总结 一句话总结:用http消息响应头中的Cache-Control来控制客户端缓存,说的是页面本身被客户端缓存,而不是重新生成的其它的非页面缓存 响应头Cache- ...

  2. Windows Live Wirter

    安装: 下载: Windows Live Writer (QQ 里) windows live writer 日志服务器发生问题 更新账户信息 从字面"editPost"我们不难看 ...

  3. 20170914xlVBA通讯公司分类汇总

    Sub 租房() Dim Wb As Workbook Dim Sht As Worksheet Dim OpenWb As Workbook Dim OpenSht As Worksheet Dim ...

  4. apiCloud 下拉刷新

    api.setRefreshHeaderInfo({ bgColor: '#fff', textColor: '#4d4d4d', },function(ret, err){ //下拉刷新时,刷新的数 ...

  5. unitest 测试集 实例

    -->baidy.py #coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import B ...

  6. dp练习2

    1, CF 808G Anthem of Berland 2, CF 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses

  7. swf 上传

    推荐这个博客:http://blog.csdn.net/hi_kevin/article/details/23430345 这个文章很不错,我推荐这个文章:http://leeon.me/upload ...

  8. kohana导入和导出

    一.导入 ini_set('memory_limit', '512M'); require_once(Kohana::find_file('vendor', 'PHPExcel/PHPExcel/IO ...

  9. bootstrap fileinput组件的使用

    组件的下载地址为:https://github.com/kartik-v/bootstrap-fileinput 比较详细的介绍可参看:http://www.jq22.com/jquery-info5 ...

  10. Oracle11g温习-第十八章:role管理

    2013年4月27日 星期六 10:52 role 的功能:简化用户的权限管理 建立角色——给角色授权——将角色授予用户/角色 2.查看系统建立的role SYS @ prod >  selec ...