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 ...
随机推荐
- String的intern()方法和java关键字、保留字
String s1 = new StringBuilder("hel").append("lo").toString(); //hello System.out ...
- 比较Class.getResource与Class.getClassLoader().getResource两种方式读取资源文件
/** * @author zhangboqing * @date 2018/7/10 */ public class FileDemo { public static void main(Strin ...
- android -------- WIFI 详解
今天简单的来聊一下安卓开发中的Wifi,一些常用的基础,主要分为两部分: 1:WiFi的信息 2:WiFi的搜索和连接 现在app大多都需要从网络上获得数据.所以访问网络是在所难免.但是在访问网络之前 ...
- Spring Batch JSON 支持
Spring Batch 4.1 开始能够支持 JSON 格式了.这个发布介绍了一个新的数据读(item reader)能够读取一个 JSON 资源,这个资源按照下面的格式: [ { &q ...
- 统计数组中各个元素出现的次数,元素取值范围为:1到N
问题描述: * 给定一个整数数组a,长度为N,元素取值范围为[1,N]. * 统计各个元素出现的次数,要求时间复杂度为O(N),空间复杂度为O(1). * 可以改变原来数组结构. 思路: * 从第 ...
- 使用saltui实现图片预览查看
项目是基于dingyou-dingtalk-mobile脚手架的一个微应用,这个脚手架使用的UI是antd-mobile,它提供了一个图片上传的组件,但是未提供图片预览的组件,在网上找了不少如何在re ...
- WCF开发实战系列一:创建第一个WCF服务 转
转 http://www.cnblogs.com/poissonnotes/archive/2010/08/28/1811064.html 在这个实战中我们将使用DataContract,Servic ...
- Leetcode 1022. 可被 K 整除的最小整数
1022. 可被 K 整除的最小整数 显示英文描述 我的提交返回竞赛 用户通过次数74 用户尝试次数262 通过次数75 提交次数1115 题目难度Medium 给定正整数 K,你需要找出可以被 ...
- JDK的bin目录下各种工具的使用说明_对不起自己,这么久没写博,抱歉
appletviewer.exe(小程序浏览器):一种执行HTML文件上的Java小程序类的Java浏览器 apt.exe:SolarisTM 操作系统和 Linux上用于处理注释的工具 extche ...
- 通过cassandra-cli客户端了解cassandra的内部数据结构
和cassandra数据库交互的方式有两种,一种是通过类似于cassandra-cli命令的thrift api,或者通过cassandra提供的cql(cassandra query lanugag ...