关于leetcode中对数组O(1)空间消耗操作的几道题
2015-04-21 09:05
197 查看
其实这几道题在leetcode中都是比较容易的,但是如果刚开始不理解题意的话可能就会进入陷阱。
整数数组中的几个操作如下面所示,无非是怎样进行数组元素的的交换。
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.
这道题是删除一个整数数组中的与给定元素相同的元素。题目提示可以将原有元素的顺序打乱,最后返回的是数组的长度
同时不能有其他的空间消耗。
其实我当初的思路还是很简单的:
我是用一个头指针,一个尾指针分别从两端开始,每一次如果前面的元素出现了elem,那么从尾指针当前位置
向前找到第一个出现的非elem的元素进行交换,这样的时间复杂度是O(m)其中m是非elem的个数,但是代码的实现上稍显复杂:
/*---------------------O(m)时间消耗,O(1)空间消耗-------------------------------------*/
//首尾两个指针,向中间扫描。用后面元素向前面元素进行替换,会改变元素的位置
//By Lingtao 2015/04/21
int removeElement(int A[], int n, int elem) {
int length = 0;
int head = 0, tail = n - 1;
while (head <= tail)
{
if (A[head] == elem)
{
while (tail > head)
{
if (A[tail] != elem)
{
swap(A[head], A[tail]);
length++;
break;
}
tail--;
}
}
else
length++;
head++;
}
cout << endl;
return length;
}
//下面的代码不用改变数组元素的顺序,但是需要进行o(n)的时间度,即需要对整个元素进行扫描一次。
简洁了许多。
int removeElement2(int A[], int n, int elem) {
int count = 0;
for (int i = 0; i<n; i++)
if (A[i] != elem)
swap(A[i], A[count++]);
return count;
}
其实,对于这种问题,解法二才是最正宗的解法,两个point一前一后,一个用来遍历数组,一个用来记录有效元素的位置。下面这道题依然是类似的题目:
Given a sorted array, remove the duplicates in place such that each element appear only once and
return the new length.
这里面要求数组元素的顺序是不能变的。那么就可以采用一中的方法。
仍然是两个指针:
int removeDuplicates(int A[], int n) {
if (n == 0 || n==1)
return n;
int count = 1;
for (int i = 1; i < n; i++)
{
if (A[i - 1] == A[i])
continue;
else
{
A[count] = A[i];
count++;
}
}
return count;
}
未完待续。
整数数组中的几个操作如下面所示,无非是怎样进行数组元素的的交换。
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.
这道题是删除一个整数数组中的与给定元素相同的元素。题目提示可以将原有元素的顺序打乱,最后返回的是数组的长度
同时不能有其他的空间消耗。
其实我当初的思路还是很简单的:
我是用一个头指针,一个尾指针分别从两端开始,每一次如果前面的元素出现了elem,那么从尾指针当前位置
向前找到第一个出现的非elem的元素进行交换,这样的时间复杂度是O(m)其中m是非elem的个数,但是代码的实现上稍显复杂:
/*---------------------O(m)时间消耗,O(1)空间消耗-------------------------------------*/
//首尾两个指针,向中间扫描。用后面元素向前面元素进行替换,会改变元素的位置
//By Lingtao 2015/04/21
int removeElement(int A[], int n, int elem) {
int length = 0;
int head = 0, tail = n - 1;
while (head <= tail)
{
if (A[head] == elem)
{
while (tail > head)
{
if (A[tail] != elem)
{
swap(A[head], A[tail]);
length++;
break;
}
tail--;
}
}
else
length++;
head++;
}
cout << endl;
return length;
}
//下面的代码不用改变数组元素的顺序,但是需要进行o(n)的时间度,即需要对整个元素进行扫描一次。
简洁了许多。
int removeElement2(int A[], int n, int elem) {
int count = 0;
for (int i = 0; i<n; i++)
if (A[i] != elem)
swap(A[i], A[count++]);
return count;
}
其实,对于这种问题,解法二才是最正宗的解法,两个point一前一后,一个用来遍历数组,一个用来记录有效元素的位置。下面这道题依然是类似的题目:
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.
这里面要求数组元素的顺序是不能变的。那么就可以采用一中的方法。
仍然是两个指针:
int removeDuplicates(int A[], int n) {
if (n == 0 || n==1)
return n;
int count = 1;
for (int i = 1; i < n; i++)
{
if (A[i - 1] == A[i])
continue;
else
{
A[count] = A[i];
count++;
}
}
return count;
}
未完待续。
相关文章推荐
- 关于leetcode中链表操作的的几道题。
- C语言学习6 :指针的定义,指针类型要合法,指针要初始化,指针做函数参数,数组和指针的通用性,指针+1所代表的空间,void * 指针,交换函数中的指针,数组和字符型指针区别,字符型指针的应用,使用指针完成字符操作函数
- 关于数组中重复元素的操作
- C/C++中关于sizeof对数组的操作
- 关于数组的几道面试题
- Sort Colors [leetcode] 扫描数组一遍,O(1)空间复杂度的解法
- LeetCode 23 Merge k Sorted Lists 高空间消耗解法与堆的解法,还需要学习堆的做法
- visual studio 关于指针与数组操作的比较
- php中关于数组中指针的操作
- leetcode:java中的数组操作
- 关于oracle 11.2.0.4 使用asm磁盘组条件下对应用数据表空间文件的rename alias操作
- Javascript关于数组的一些操作
- 关于数组的几道面试题
- 转:关于数组的几道面试题
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
- php入门学习知识点五 关于php数组的几个基本操作
- 设数组data[m]作为循环队列的存储空间。front为队头指针,rear为队尾指针,则执行出队操作后其头指针front值为()
- Longest Increasing Subsequence(最长增长子数列)-LeetCode关于数组的思路和技巧
- 关于数组的几道面试题
- 关于 android Intent 传对象和对象数组的一些操作