LeetCode Remove Element删除元素

 1 class Solution {
 2 public:
 3 int removeElement(int A[], int n, int elem) {
 4     int *p=A,*e=&A[n-1];
 5     int i,num=n;
 6     for(i=0;i<n;i++){    //一共要对比n次,不能用n来处理,会影响循环
 7         if(*p==elem){
 8             if(p==e)    //已经处理到最后一个相同,只需总数减1
 9                 num--;
10             else{
11                 *p=*e;
12                 e--;
13                 num--;
14             }
15         }
16         else
17             p++;
18     }
19     return num;
20 }
21 };

题意:给一个整型数组(无序),删除规定的某一元素,返回剩下的元素个数。

思路:两个指针,一个从头扫,一个从尾扫,一共要对比n次,因为有n个元素。从头扫到该删除的元素就用后面扫起的元素代替掉。

注意:数组是无序的,你可以随意将顺序更改,只要得到一个结果:规定删除的元素值不能出现新数组中即可。利用此特点,不用每次扫到一个要删除的元素就将该元素之后的所有元素往前移动了。

时间: 2024-11-05 14:59:10

LeetCode Remove Element删除元素的相关文章

[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. 题意:删除给定的数,然后返回新的长度. 思路:这题的思路和sort colors差不多,是其简化版.大致的思路是:

leetCode 27.Remove Element (删除元素) 解题思路和方法

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. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

lintcode 容易题:Remove Element 删除元素

题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响.  样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 返回 4 并且4个元素的新数组为[0,0,0,2] 解题: Java程序: public class Solution { /** *@param A: A list of integers *@param elem: An integer *@return: The new length aft

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. 中文:给定一个数组和一个数值,去除这个数值所有出现位置,并返回新数组的长度. 元素的顺序可以改变.除了新的长度,你

LeetCode: Remove Element [026]

[题目] 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. [题意] 删除数组中指定的值.不关心在新数组的后面即数组尾部留下了什么值. [思路] 思路同Remove

[LeetCode] Remove Element [20]

题目 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. 原题链接(点我) 解题思路 给一个数组和一个数字,移除该数字在数组中所有出现的地方. 这是一个非常简单的题目

LeetCode 27.Remove Element 数组元素删除

27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.

[LeetCode] Remove Element 分析

Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/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. I

Remove Element(删除重复元素,循环删除2个及2个以上元素)

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. 时间复杂度O(n),空间复杂度O(1).解题思路,主要就是双指针的思想,一个为游标遍历,一个为新数组的最后一个元素