Remove duplicates from array II
//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]. //Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
// For example, Given sorted array A = [1,1,1,2,2,3],
// Your function should return length = 5, and A is now [1,1,2,2,3] #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;
int flag = 0; while(pn < n)
{
if (a[p] == a[pn])
{
if(flag <1)
{
p++;
a[p] = a[pn];
pn++;
flag++;
}
else
{
pn++;
flag ++;
}
}
else
{
p++;
a[p] = a[pn];
pn++;
flag = 0;
}
}
return p+1;
}
}; class Solution
{//更简洁的办法
public:
int removeDuplicates(int A[], int n)
{
if(n<2)return n;
int index = 2;
for (int i = 2; i < n; i++)
{
if (A[i] != A[index-2])
{
A[index] = A[i];
index++;
}
}
return index++;
}
}; int main()
{
int a[12] = {1,1,2,2,2,4,4,7,7,7,8,9}; Solution s;
int t = s.removeDuplicates(a,12);
cout<<t<<endl;
for (int i = 0; i<t; i++)
{
cout<<a[i]<<endl;
}
return 0;
}
Remove duplicates from array II的更多相关文章
- Remove duplicates from array
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- Remove Duplicates from Sorted Array I&&II——怎样防超时
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
随机推荐
- 终于把joomla 的 protostar 模版的菜单,从垂直改到水平了
protostar-applying-menu-class-suffixes-horizontal-vs-vertical-menus.html joomla 3.7.5 附带的这个template ...
- php oracle数据库NCOLB字段ORA-01704
php oracle数据库NCOLB字段ORA-01704 对clob更新 ORA-01704: 字符串文字太长 解决办法:把字符赋值给一个变量,然后赋值update语句 declarev_clob ...
- 判断一个点在多边形的内部C++
/* 原理: 将测试点的Y坐标与多边形的每一个点进行比较, ** 会得到测试点所在的行与多边形边的所有交点. ** 如果测试点的两边点的个数都是奇数个, ** 则该测试点在多边形内,否则在多边形外. ...
- LeetCode--371--两整数之和
问题描述: 方法: class Solution(object): def getSum(self, a, b): """ :type a: int :type b: i ...
- inflate()引发NullPointerException
有时候我们在infalete的时候明明什么都对为什么它会提示出错 原意是你的资源layout出错了 注意看有没有把View写成view 这个View应该大写!V而不是小写v 踩坑踩了两次了!上次以为是 ...
- android -------- Eclipse下的NDK配置环境
NDK 全称是Native Development Kit,是一个让开发人员在Android应用中嵌入使用本地代码编写的组件的工具集 原生开发工具包 (NDK) 是一组可让您在 Android 应用中 ...
- 动态加载DataGrid表头及数据
初始化表头 js生成前端 /*初始化表头*/ function initDataGridTitle(id) { $.ajax({ url: '/${appName}/report/***/***', ...
- 迷宫最短路径问题的dfs,bfs实现
迷宫的最短路径 给定一个大小为 N×M的迷宫.迷宫由通道和墙壁组成,每一步可以向邻接的上下左右四格的通道移动.请求出从起点到终点所需的小步数.请注意,本题假定从起点一定可以移动到终点 限制条件:N,M ...
- sparklyr-R语言访问Spark的另外一种方法
Connect to Spark from R. The sparklyr package provides a complete dplyr backend. Filter and aggregat ...
- [转载]Python使用@property装饰器--getter和setter方法变成属性
原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...