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 ...
随机推荐
- 常用的JSTL函数
下面是JSTL中自带的方法列表以及其描述 函数名 函数说明 使用举例 fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(na ...
- Asp.net MVC 4新项目中创建area的后续操作
Asp.net MVC 4新项目中创建area后,往往HomeController与area的HomeController路由发生混淆,需要手工设置一些地方避免mvc无法识别默认路由的状况. 无废话具 ...
- Effective Java 20 Prefer class hierarchies to tagged classes
Disadvantage of tagged classes 1. Verbose (each instance has unnecessary irrelevant fields). 2. Erro ...
- Java中关于 BigDecimal 的一个导致double精度损失的"bug"
背景 在博客 恶心的0.5四舍五入问题 一文中看到一个关于 0.5 不能正确的四舍五入的问题.主要说的是 double 转换到 BigDecimal 后,进行四舍五入得不到正确的结果: public ...
- swift函数的用法,及其嵌套实例
import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...
- linux内核编译,配置本机驱动
1.前言 编译linux内核失败的原因很多时候就是驱动选错,适合自己本机的驱动没编译进去.面对特殊平台(或者有些洁癖者,我就是^_^),要编译精简内核,只要本机驱动,其他都不需要.面对内核里面这么多 ...
- Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- Check list
greenplum
- 搜索+剪枝 POJ 1416 Shredding Company
POJ 1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5231 Accep ...
- tarjan算法求桥双连通分量 POJ 3177 Redundant Paths
POJ 3177 Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12598 Accept ...