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

思路:

我用两个游标分别记录新数组的下一个位置newpos和原数组要判断的下一个位置pos。如果pos位置的数字和newpos - 1 的一样则跳过

关键:比较是否一样时与新的数组的目前最后一个数字比。

int removeDuplicates(int A[], int n) {
if(n == ) return ;
int newpos = , pos = ;
while(pos < n)
{
if(A[pos] == A[newpos - ]) pos++;
else A[newpos++] = A[pos++];
}
return newpos;
}

大神超短的代码:

int count = ;
for(int i = ; i < n; i++){
if(A[i] == A[i-]) count++;
else A[i-count] = A[i];
}
return n-count;

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

允许两个重复的

思路:与上面其实都一样,就是把判断条件改为与上两个数字相同就跳过。注意,要与新数组最后两个数字比较。

int removeDuplicatesTwice(int A[], int n) {
if(n <= ) return n;
int pos = ; //新数组的下一个判断位置
for(int i = ; i < n; i++)
{
if(!(A[i] == A[pos - ] && A[i] == A[pos - ])) //这里是关键,要和pos-1 和 pos-2比较。pos对应的出现两次才是真的两次了。不能和i-1和i-2比,因为可能在前面这两个位置的数字已经改变了。
{
A[pos++] = A[i];
}
}
return pos;
}

有人写了一个问题泛化到可以重复k次的代码:

int removeDuplicates(int A[], int n, int k) {
if (n <= k) return n;
int i = , j = ;
int cnt = ;
while (j < n) {
if (A[j] != A[j-]) { //其实我觉得这里写成A[j] != A[i - 1] 更好
cnt = ;
A[i++] = A[j];
}
else {
if (cnt < k) {
A[i++] = A[j];
cnt++;
}
}
++j;
}
return i;
}

【leetcode】Remove Duplicates from Sorted Array I & II(middle)的更多相关文章

  1. 【leetcode】Remove Duplicates from Sorted Array II

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

  2. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  3. 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)

    这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...

  4. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  5. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. 【Leetcode】【Easy】Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  8. 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)

    1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  9. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

随机推荐

  1. 更改vs自带的模板

    路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\20 ...

  2. 笔记之Python网络数据采集

    笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...

  3. Java-Linux系统中搭建开发环境

    准备工作: 0.虚拟机中的系统→{RHEL-I386} 1.JDK→{首先要知道下载哪个版本" [zf@string ~]$ getconf LONG_BIT ":".t ...

  4. 采用Atlas+Keepalived实现MySQL读写分离、读负载均衡【转载】

      文章 原始出处 :http://sofar.blog.51cto.com/353572/1601552 ============================================== ...

  5. linux C之getchar()非阻塞方式

    参考链接: http://blog.csdn.net/zydlyq/article/details/50963360 #include "../include/CommUart.h" ...

  6. PHPStorm Xdebug配置

    下载PHSTORM https://download.jetbrains.com/webide/PhpStorm-2016.1.2.exe http://idea.lanyus.com/查找授权服务器 ...

  7. CentOS 6.5 zabbix 3.0.4 SendEmail报警

    官方介绍:http://caspian.dotconf.net/menu/Software/SendEmail/ 1.sendEmail部署 下载安装包到本地.解压 [root@localhost S ...

  8. python 函数的调用 和执行 小知识

    1.符号表 执行一个函数会引入一个用于函数的局部变量的新符号表. 更确切地说, 函数中的所有的赋值都是将值存储在局部符号表: 而变量引用首先查找局部符号表, 然后是上层函数的局部符号表, 然后是全局符 ...

  9. C++笔试题(转)

    http://blog.csdn.net/hxz_qlh/article/details/16864567 这里面列举的题考察的东西都非常细,包括strcpy,字符串,大.小端的判断,很容易犯错,值得 ...

  10. opencv中的视频的读入

    #include"stdafx.h"#include"opencv2/opencv.hpp" using namespace cv;int g_slider_p ...