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. 关于一些学习html和css的笔记

    一.Html简介 全写: HyperText Mark-up Language  译名: 超文本标识语言  简释:一种为普通文件中某些字句加上标示的语言,其目的在于运用标签(tag)使文件 达到预期的 ...

  2. Css学习笔记 (一)

    这几天写了点CSS,大概总结一下,很凌乱,只是加深一下自己的认识. background-position background-position是以容器的左上角为0,0定位,支持(left,righ ...

  3. SVG 2D入门10 - 滤镜

    滤镜称得上是SVG最强大的功能了,它允许你给图形(图形元素和容器元素)添加各种专业软件中才有的滤镜特效.这样你就很容易在客户端生成和修改图像了.而且滤镜并没有破坏原有文档的结构,所以维护性也很好.   ...

  4. javaweb---html标签

    img标签

  5. 设置ASP.NET页面的运行超时时间详细到单个页面及站点

    这篇文章主要介绍了如何设置ASP.NET页面的运行超时时间,包括全局超时时间.单个站点超时时间.单个页面请求超时时间,需要的朋友可以参考下     全局超时时间 服务器上如果有多个网站,希望统一设置一 ...

  6. HDU 2276

    http://acm.hdu.edu.cn/showproblem.php?pid=2276 矩阵乘法可以解决的一类灯泡开关问题 /* 转移关系为 now left now* 1 0 1 1 1 0 ...

  7. attention 机制

    参考:modeling visual attention via selective tuning attention问题定义: 具体地, 1) the need for region of inte ...

  8. ibatis批量操作补充

    ibatis批量操作  这文章的补充吧. review代码的时候发现一个页面应用排序设置功能,原先代码中,进行了循环update实现.虽然应用较少,不过无谓浪费数据库链接实在不是一个好的想法. 所以, ...

  9. iOS的URL处理

    前两天处理iOSapp过程中(我是用swift语言写的,资料较少),被一个“字符串”搞了一晚上的时间到第二天才处理好,在此记下,望见过此文的学生有一天遇到该情况能三分钟搞定不浪费时间: 先看如下代码 ...

  10. LeetCode()Minimum Window Substring 超时,但觉得很清晰。

    我的超时思路,感觉自己上了一个新的台阶,虽然超时了,但起码是给出了一个方法. 遍历s 一遍即可,两个指针,当找到了一个合格的字串后,start 开始走,直到遇到s[start]在t中 如果不符合,en ...