50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
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的更多相关文章
- [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 ...
- Why is processing a sorted array faster than an unsorted array?
这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...
- 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. 思路 ...
- 删除排序数组中的重复数字 II · Remove Duplicates from Sorted Array II
重复一次 [抄题]: 给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. [思维问题]: [ ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- [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 ...
- [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 ...
- 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 ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
随机推荐
- easyui 之ComboTree 用法Demo
实现效果如下: HTML部分: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="ser ...
- 计算第k个质因数只能为3,5,7的数
英文描述:Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7 思路: ...
- JVM-并发-Java 内存模型
Java内存模型 (1). 主内存与工作内存 Java内存模型规定了所有的变量都存储在主内存中. 每类线程的变量的主内存副本拷贝,线程对变量的所有操作(读操作,赋值操作等)都必须工作内存中进行,而不能 ...
- (转)PhoneGap开发环境搭建
(原)http://www.cnblogs.com/Random/archive/2011/12/28/2305398.html PhoneGap开发环境搭建 项目中要用PhoneGap开发,了解 ...
- sql基本增删改查语法
sql语法学习(适合新手) 1.插入数据 语法格式: INSERT [INTO] <表名> [列名] VALUES <值列表> insert into students(sna ...
- [Java Basics] Collection
除了Java collection class/interface外,方便的有Google guava的utility class: Lists/Sets/Maps/Queues, 用它们可以方便地创 ...
- 关于VC工程的组成
刚刚建立的工程,其中 .vcxproj文件是生成的工程文件,它包含当前工程的设置和工程所包含的文件等信息..vcxproj.filters文件存放工程的虚拟目录信息,也就是在解决方案浏览器中的目录结构 ...
- 从零开始学习Node.js例子三 图片上传和显示
index.js var server = require("./server"); var router = require("./router"); var ...
- vim编辑下Python2.0自动补全
Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim python自动补全插件:pydiction 可以实现下面python代码 ...
- A - Humble Numbers
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...