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 ...
随机推荐
- 雷林鹏分享:现实生活中的 XML
现实生活中的 XML 如何使用 XML 来交换信息的一些实例. 实例:XML 新闻 XMLNews 是用于交换新闻和其他信息的规范. 对新闻的供求双方来说,通过使用这种标准,可以使各种类型的新闻信息通 ...
- 移动端 meta 必备
将页面宽度到跟手机宽度比例相同,在手机上不能用手缩放 <meta name="viewport" content="width=device-width" ...
- 模拟curl函数
只要需要调用微信的网址,就需要模拟curl请求 $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_cr ...
- python and or的理解规则
>>> 'a' and 'b' 'b' >>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c’ 解释:在布尔上 ...
- selenium配置Chrome驱动
1.http://chromedriver.storage.googleapis.com/index.html chrome下载驱动地址 和对应的版本驱动,不用FQ 2.配置方法:如在e盘创建一个 ...
- 『TensotFlow』RNN中文文本_下_暨研究生开学感想
承前 接上节代码『TensotFlow』RNN中文文本_上, import numpy as np import tensorflow as tf from collections import Co ...
- 【其他】【Restful】【1】简单了解Restful概念
内容: REST是一种设计风格,不是一种标准,是一种思想.符合REST原则的架构方式即可称为RESTful. 在Restful之前的操作:http://127.0.0.1/user/query/1 G ...
- 深拷贝的原生js实现
面试时被问到怎么实现深拷贝,想都没想就用var obj2=JSON.parse(JSON.stringify(obj1))来实现.但面试官却要我用循环写出来,那就只能用递归了.可惜当时一下子忘了判断是 ...
- stl中的for_each() 函数的注意事项
#include<iostream> using namespace std; #include"vector" #include"algorithm&quo ...
- css单位分析、颜色设置与调色板
CSS单位分析 px:单位代表像素,1px代表一个像素点. %:设置子元素为父容器的占比. em:代表该元素中一个字体所占字符,常用在文字首行缩进.其具有继承性. rem:始终代表html中的字符所在 ...