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 ...
随机推荐
- requests库使用:通过cookie跳过验证码登录,并用Session跨请求保持cookie
拿我平时测试的一个系统为例,从UI层面来说必须先登录才可以进行后续操作,但是我在测试接口文档提供的接口时,发现并不需要登录,每个接口只要传参就可以正常返回.原因是我们这边专门弄了一个接口包来统一管理常 ...
- Go语言学习之4 递归&闭包&数组切片&map&锁
主要内容: 1. 内置函数.递归函数.闭包2. 数组与切片3. map数据结构4. package介绍 5. 排序相关 1. 内置函数.递归函数.闭包 1)内置函数 (1). close:主要用来关闭 ...
- [mybatis-spring] Transaction 事务/事务处理/事务管理器
使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...
- 常见的ORACLE语句
基本 --新建表: create table table1( id varchar(300) primary key, name varchar(200) not null); --插入数据 inse ...
- Introduction to dnorm, pnorm, qnorm, and rnorm for new biostatisticians
原文:Introduction todnorm,pnorm,qnorm, andrnormfor new biostatisticians Today I was in Dan’s office ho ...
- 20165303学习基础和C语言基础调查
20165303学习基础和C语言基础调查 技能学习心得 我认为我的乒乓球打的还不错,不能说非常好,但是基本的一些技巧都还是会的,小时候爸爸就非常爱看乒乓球比赛,有时候也带着我一起看,最开始看的时候我发 ...
- spring boot ----> jpa连接和操作mysql数据库
环境: centos6.8,jdk1.8.0_172,maven3.5.4,vim,spring boot 1.5.13,mysql-5.7.23 1.引入jpa起步依赖和mysql驱动jar包 &l ...
- python记录day24 模块的语法
使用模块的两种方式: import xxx from xxx import xxx 使用模块的优点: 1.实现代码的重用 2.可以把代码分类 一.import 导入模块的时候系统会做三件事 1.先去s ...
- 『MXNet』第十一弹_符号式编程初探
一.符号分类 符号对我们想要进行的计算进行了描述, 下图展示了符号如何对计算进行描述. 我们定义了符号变量A, 符号变量B, 生成了符号变量C, 其中, A, B为参数节点, C为内部节点! mxne ...
- 精华 selenium_webdriver(python)调用js脚本
#coding=utf-8 from selenium import webdriver import time driver = webdriver.Firefox() driver.get(&qu ...