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. 爱壁纸 站立会议(六)--Spring阶段总结会议

    爱壁纸 站立会议(六)--Spring阶段总结会议 一.会议时间 2014年4月15日 星期三 21:00-21:20 今天是spring阶段最后一天,大家都对这一星期的任务概况做出了总结所以时间稍微 ...

  2. MSSQL常用函数

    declare 定义变量 set 为变量赋值 SUBSTRING()函数 SUBSTRING ( expression, start, length ) expression 字符串.二进制字符串.文 ...

  3. 苹果5S指纹扫描识别传感器Touch ID有利于iPhone的安全性

    iPhone5S新增的指纹扫描识别传感器 Touch ID,黑客花了大量的时间表明指纹验证是可以被破解的.即使它可能被黑客攻击,对iPhone5S的安全性而言,仍然具有极大的好处. 为什么一个容易被破 ...

  4. For-Each循环

    For-Each循环也叫增强型的for循环,或者叫foreach循环. For-Each循环是JDK5.0的新特性(其他新特性比如泛型.自动装箱等). For-Each循环的加入简化了集合的遍历. 语 ...

  5. WCF 服务器调用回调函数 单程-双程操作模式:(待补充Demo)

    服务器端Server 实现回调接口Interface定义.客户端实现回调接口Interface实现,从而实现服务器端通过  var channel = OperationContent.Current ...

  6. Modbus工业协议在Android中的应用

    现在工业信息画发展,很多工厂都需要做信息化展示,通常都是利用Android一体机来进行展示和交互. Modbus协议是全球第一个用于工业现场的总线协议,与外设交互可以采用串口通信,tcp等方式:通常在 ...

  7. Gym 100960G (set+树状数组)

    Problem Youngling Tournament 题目大意 给一个序列a[i],每次操作可以更改一个数,每次询问 将序列排序后有多少个数a[i]>=sum[i-1]. n<=10^ ...

  8. Why NSAttributedString import html must be on main thread?

    The HTML importer should not be called from a background thread (that is, the options dictionary inc ...

  9. Python 基礎 - pyc 是什麼

    Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...

  10. JSP 动作元素

    JSP动作元素 1.  动作元素分类 用来动态的包含文件.网页跳转及使用JavaBean组件等. 语法:<jsp:XXX />或者<jsp:XXX></jsp:XXX&g ...