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. Redisson-Parent 2.5.0 和 3.0.0 发布

    Redisson-Parent 2.5.0 和 3.0.0 发布了,Redisson 是基于 Redis 服务之上构建的分布式.可伸缩的 Java 数据结构,高级的 Redis 客户端. Rediss ...

  2. JS form表单图片上传

    // 点击file 类型的input 触发的方法 function changesProvider(){ // fileProvider -> input中的name属性值 var f = do ...

  3. [蟒蛇菜谱]Python获取任意xml节点的值

    # -*- coding: utf-8 -*- import xml.dom.minidom ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE class Simple ...

  4. UVA 10816 + HDU 1839 Dijstra + 二分 (待研究)

    UVA 题意:两个绿洲之间是沙漠,沙漠的温度不同,告诉起点,终点,求使得从起点到终点的最高温度最小的路径,如果有多条,输出长度最短的路径: 思路:用最小费用(最短路径)最大流(最小温度)也能搞吧,但因 ...

  5. Boundaries

    Using Third-Party Code There is a natural tension between the provider of an interface and the user ...

  6. Android关联源码support-v4,v7,v13源码(转)

    在Android实际开发过程中往往会遇到使用v4,v7或v13兼容包中的一些类如ViewPager,Fragment等,但却无法关联源码. 在网上搜索之后,有很多办法,这里只向大家介绍一种,我用的觉得 ...

  7. C++学习笔记23:库

    静态库(Archives) 后缀一般为"*.a" 使用两个目标文件创建单一静态库的编译与链接命令:ar cr libtest.a  test1.o test2.o 链接器搜索静态库 ...

  8. Linux 2>&1理解(转)

    2>&1使用一 相关知识1)默认地,标准的输入为键盘,但是也可以来自文件或管道(pipe |).2)默认地,标准的输出为终端(terminal),但是也可以重定向到文件,管道或后引号(b ...

  9. oracle数据库解析json格式

    随着非关系型数据大规模使用,以json格式产生的数据也出现在我所管理的Oracle数据库的CLOB字段里面,使用过程中就需要解析出指定键的值. 使用了最新版本 如果Oracle版本为12.1.0.2的 ...

  10. pip install 出现报asciii码错误的问题

    原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码. 解决办法是: python目录 Python27\Lib\site-packages 建一个文件sit ...