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

思路:方法1:与前面相同,则删除。关键是移动元素不要出错。(不可取: 1360ms)

class Solution {
public:
int removeDuplicates(int A[], int n) {
int i = 1;
while(i < n) {
if(A[i] == A[i-1]) {
int j = i;
while(j < n-1)
A[j++] = A[j+1];
--n;
}
else ++i;
}
return n;
}
};

方法2:(优)不用移动元素。设置自增变量,如 A 中该元素与前一元素不同,则放入该变量位置,变量增 1 .(132 ms)

class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n < 2) return n;
int id = 1;
for(int i = 1; i < n; ++i)
if(A[i] != A[i-1]) A[id++] = A[i];
return id;
}
};

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

思路:该位置的前两个重复时,设置标志 repeat = true.

方法1:(移动元素: 136ms)

void remove(int A[], int id, int& n) {
while(id < n-1) A[id++] = A[id+1];
--n;
}
class Solution {
public:
int removeDuplicates(int A[], int n) {
bool repeat = false;
int i = 1;
while(i < n) {
if(repeat && A[i] == A[i-1]) { remove(A, i, n); continue; }
if(A[i] == A[i-1]) repeat = true;
else repeat = false;
++i;
}
return n;
}
};

方法2: (优:不移动元素:80ms)

class Solution {
public:
int removeDuplicates(int A[], int n) {
if(n < 3) return n;
bool repeat = false;
int id = 1;
for(int i = 1; i < n; ++i) {
if(repeat && A[i] == A[i-1]) continue;
if(A[i] == A[i-1]) { repeat = true; A[id++] = A[i]; }
else { repeat = false; A[id++] = A[i]; }
}
return id;
}
};

Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

思路: 同上。

class Solution {
public:
int removeElement(int A[], int n, int elem) {
int L = 0;
for(int i = 0; i < n; ++i) {
if(A[i] == elem) continue;
A[L++] = A[i];
}
return L;
}
};

50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element的更多相关文章

  1. [Swift]LeetCode80. 删除排序数组中的重复项 II | Remove Duplicates from Sorted Array II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  2. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  3. 108.Convert Sorted Array to Binary Search Tree(Array; Divide-and-Conquer, dfs)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路 ...

  4. 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II

    重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...

  5. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  6. [Swift]LeetCode82. 删除排序链表中的重复元素 II | Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

  8. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

随机推荐

  1. adb push和pull使用

    1.运行cmd> 进入adb.exe目录 2.>adb connect ip 3.>adb remount 4.>adb push 本地apk全路径 /system/app/ ...

  2. HRBUST 1867 差分+BIT

    我在群上看到的某道题,貌似用的是线段树,因为前几天遇到差分,再用BIT动态维护一下前缀和,感觉可做就A了. 加了个读优就Rank1啦! 某个不常见的题库,还是把题目拿下来把.. Description ...

  3. svn Q&A

    Q1:在svn commit的时候,会出现某某文件 is missing.这是因为此次提交时:远程repository中并没有该文件,而且本地repository也没有该文件. 具体原因: 1.可能因 ...

  4. 关于在win7内集成usb3.0驱动。

    mac air 装了win7但是折腾良久还是无法升级,只能是重新安装. 很蛋疼.bootcamp 老是找不到驱动.只能是手动分区后U盘引导安装. 驱动的下载,直接在Os x 下用bootcamp 下载 ...

  5. Linux命令行与图形界面切换方法

    1.实时切换 1.1 命令行->图形 startx 1.2 图形->命令行 Ctrl+Alt+F1--F6 2.启动默认 2.1 启动进入命令行 修改/etc/inittab文件 &quo ...

  6. yii去除index.php的入口脚本显示为seo友好的url

    1.去除入口脚本需要在重写url,如果你的webserver软件时Apache的话,必须配置httpd.conf,搜索“LoadModule rewrite_module modules/mod_re ...

  7. 2.b统计字符串长度

    import java.util.*;public class Main {    public static void main(String args[]){    String a;    Sc ...

  8. 用ABBYY提取文本和表格的方法

    在ABBYY FineReader 12 OCR文字识别软件中,有一个插件ABBYY Screenshot Reader,通常情况下与ABBYY FineReader 12一起安装到计算机中,它是一款 ...

  9. 【转载】开发备必:WEB前端开发规范文档

    规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必 须按本文档规范进行前台页面开发. 本文档如有不对或者 ...

  10. List remove注意点

    public class ListTest { public static void main(String[] args) { // TODO Auto-generated method stub ...