LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted 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]
.
分析:从数组的第二个元素开始遍历,把和上一个位置的值不同的的元素保存下来,注意一下n == 0的情形(这里我们直接是inplace操作)
class Solution {
public:
int removeDuplicates(int arr[], int n) {
if (n == ) {arr = NULL;return ;}
int index = ;
for(int i = ; i < n; i++)
if(arr[i] != arr[i-])
arr[index++] = arr[i];
return index;
}
};
LeetCode:Remove Duplicates from Sorted Array II
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]
.
分析:和上一题相似,从数组的第三个元素开始遍历,把和前一个的前一个(A[i-2])的值不同的元素保存下来,这里需要注意的是原来的A[i-2]的值可能已经被覆盖了,比如1,1,1,2,2,3遍历到第4个元素2时,需要保存,并且保存在了第三个位置数组变成了1,1,2,2,2,3,遍历到第5个元素2时,前一个的前一个元素本来是1,但是前面已经被2给覆盖了,所有我们需要保存上一次被覆盖元素的索引和值。 本文地址
class Solution {
public:
int removeDuplicates(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//只要当前元素和前一个的前一个不同就保留
if(n == ) {A = NULL;return ;}
if(n <= )return n;
int index = ;
int lastChange, lastChangeIndex = -; //上一次被覆盖的位置和被覆盖的值
for(int i = ; i < n; i++)
{
int prepre = A[i-];
if(i- == lastChangeIndex)prepre = lastChange;//原来的A[i-2]有可能被覆盖了
if(A[i] != prepre)
{
lastChangeIndex = index;
lastChange = A[index];
A[index++] = A[i];
}
}
return index;
}
};
还有一种更优雅的方法是,当我们需要留下某个元素时,先暂时保存好,等到下一轮再覆盖,这样我们找前一个的前一个元素A[i-2]时,就不会出现A[i-2]原来的值被覆盖的情况。
class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n==)return ;
if(n==)return ;
int num=,i,temp=A[];
for(i=;i<n;++i)
if(A[i]!=A[i-])
{
A[num++]=temp;
temp=A[i];
}
A[num++]=temp;
return num;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3461401.html
LeetCode:Remove Duplicates from Sorted Array I II的更多相关文章
- [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 II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [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 II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- 【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 ...
随机推荐
- tomcat部署
因为之前一直用的JBoss服务器,今天测试了一下想换成tomcat,然后就考了一个现成的tomcat,然后将jboss下的项目复制了一个到tomcat下,然后改了一下文件名,结果启动时报错 java. ...
- 记录ubuntu下的svn一些操作
1.install svn serversudo apt-get install subversion 2.make repositorysudo mkdir /home/.svnsudo mkdie ...
- 肯爹的 StringUtils.isNumeric(String str)
在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...
- mac下 home-brew安装及php,nginx环境安装及配置
Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...
- jQuery Form 表单提交插件-----ajaxSubmit() 的应用
Form Plugin API 里提供了很多有用的方法可以让你轻松的处理表单里的数据和表单的提交过程. 测试环境:部署到Tomcat中的web项目. 一.ajaxSubmit() 介绍 立即通过AJ ...
- ElasticSearch Filter Aggregations
类似于sql语句中where子句的作用 { "query": { "match_all": {} }, "aggs": { "ag ...
- Keepalived高可用集群搭建(转载linuxIDC)
1.Keepalived简介 Keepalived是一个基于VRRP协议来实现的WEB服务高可用方案,可以利用其来避免单点故障.使用多台节点安装keepalived.其 他的节点用来提供真实的服务,同 ...
- D_S 循环队列的基本操作
// main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...
- 循环 wxl
#include <cstdio> #include <cstring> #include <string> #include <algorithm> ...
- 【C#】SQL数据库助手类2.0(自用)
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...