[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差不多,是其简化版。大致的思路是:使用两个指针,指针l 指前,指针 r 指后,遍历数组,遇到给定的数,则将指针 l 指向的元素和r指向的元素交换,每交换一次r--一次,然后重新从指针l处重新遍历;若不是给定的数,则直接跳过即可。代码如下:

 1 class Solution {
 2 public:
 3     int removeElement(int A[], int n, int elem)
 4     {
 5         int count=0;
 6         if(n<1) return 0;
 7         int l=0,r=n-1;
 8         while(l<=r)
 9         {
10             if(A[l]==elem)
11             {
12                 swap(A[l],A[r])
13                 r--;
14                 count++;
15             }
16             else
17                 l++;
18         }
19         return n-count;
20     }
21 };

思路二:从前向后遍历数组,遇到给定数,记下其位置,将其和后面第一个不为给定数交换,即可。代码如下:

 1 class Solution
 2 {
 3 public:
 4     int removeElement(int A[],int n,int elem)
 5     {
 6         int count=0;
 7         for(int i=0;i<n;++i)
 8         {
 9             if(A[i]==elem)
10                 count++;
11             else if(count>0)  //避免多个连续
12                 A[i-count]=A[i];
13         }
14         return n-count;
15     }
16 }
时间: 2024-08-24 06:16:44

[Leetcode] remove element 删除元素的相关文章

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 nu

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).解题思路,主要就是双指针的思想,一个为游标遍历,一个为新数组的最后一个元素