Remove duplicates from 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]. #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的更多相关文章
- Remove duplicates from array II
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- [MySQL] 中 Schema和Database的区别
在MySQL中,schema和database是同义词. CREATE SCHEMA和CREATE DATABASE是等效的. 但是其他的数据库产品(几乎所有数据库)有所不同.在oracle数据库产品 ...
- MyEclipse配置Tomcat服务器(最简单的配置过程)
MyEclipse配置Tomcat服务器比较简单,在这里直接给出简要的配置步骤了,相信大家都能很容易明白…… 1.Window->Preferences 2.根据你的Tomcat版本找到对应的T ...
- linux进程管理之进程查看
查看进程 process ====================================================================================了解如 ...
- 网站访问出现 ------ Can not write to cache files, please check directory ./cache/ .
最近在搞微商城时,突然出现了Can not write to cache files, please check directory ./cache/ .这样一个提示, 但最近好像没搞什么大动作,怎么 ...
- 一个不该写的bat
新建bat文件,放到D盘根目录 %0|%0 进入到C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Program ...
- Rest_framework 和路由配置(一)
简介 Django REST framework是一个建立在Django基础之上的Web 应用开发框架,可以快速的开发REST API接口应用. Rest_framework 核心思想: 缩减代码. ...
- 微信小程序发起微信支付
点击链接查看详情:(支付中配置参数需要从后台得到->签名需要从微信申请才可以得到) https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.h ...
- php 递归删除文件夹
/*** 递归删除文件方法一 param $path 文件路径 **/ function delAll($path){ $path=str_replace('\\','/',$path);//去除反斜 ...
- Vrrp和Hsrp的区别
VRRP原理协议简述简单来说,VRRP是一种容错协议,它为具有组播或广播能力的局域网(如以太网)设计,它保证当局域网内主机的下一跳路由器出现故障时,可以及时的由另一台路由器来代替,从而保持通讯的连续性 ...
- Cassandra标准列和超级列
列(column)是Cassandra数据模型中的最基本的数据结构单元.列是一个由列名(key).值(value).时间戳(timestamp)构成的三元组.在关系型数据库中,你需要先定义列的名称和和 ...