//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的更多相关文章

  1. Remove duplicates from array

    //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

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 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 ...

  4. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  5. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 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 ...

  9. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

随机推荐

  1. Axure 第一次交互 实现跳转页面

  2. 网络cmd命令

    1.ping ip; 检测IP地址是否相通 ping命令的常用参数选项 ping IP -t:连续对IP地址执行ping命令,直到被用户以Ctrl+C中断. ping IP -l 2000:指定pin ...

  3. 20190104xlVBA_在课表里标记自己的课程

    Sub TagMyCourses() Const HEAD_ROW = 3 With ActiveSheet endrow = .Cells.Find("*", .Cells(1, ...

  4. 对比react和vue

    相同点 都有组件化开发和virtual DOM(具体实现方式不同) 都支持props进行父子组件间数据通信 都支持数据驱动,不直接操作真实DOM,更新状态数据,界面自动更新 都支持服务器渲染 都支持n ...

  5. 2.7 UML状态图

    问题的引出 顺序图是对对象的动态行为进行建模,有多个对象的参与.协作 问题: 当需要考察单个实体的动态行为时, 用UML如何来建模呢? 什么是单个实体:一个软件系统?一个子系统?一个模块.一个构件? ...

  6. Fiddler 4 界面功能介绍

    由于本人工作接触Web测试,所以我从网上找的资料,学习了解web测试内容,然后自己整理汇总的随笔,如文章中有不足的地方,请大家多多指教:或者文章内容与他人相似,望见谅 主界面: 工具栏 file:用于 ...

  7. project euler113

    project euler 113 对于1个数字,如果他数位不减或者不增称为bouncy number,比如1233,33210.统计1-10^100中的bouncy number   思路:分为两种 ...

  8. mysql 8.0.12 修改root密码

    use mysql: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码'; FLUSH PRIVI ...

  9. python-day73--django-用户验证

    一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 .authenticate()  ...

  10. PAT 1031 Hello World for U

    1031 Hello World for U (20 分)   Given any string of N (≥) characters, you are asked to form the char ...