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 Duplicates from Sorted Array

【代码】

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        if(n==0)return 0;
        int newArrayTail=-1;    //与去重不同,本题中第一个值就可能会被删除
        int pointer=0;
        while(pointer<n){
            if(A[pointer]==elem)pointer++;
            else {A[++newArrayTail]=A[pointer];pointer++;}
        }
        return newArrayTail+1;
    }
};

LeetCode: Remove Element [026]

时间: 2024-09-30 11:54:22

LeetCode: Remove Element [026]的相关文章

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 [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] 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

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. 这道题比较简单直接看代码,这里多写几个版本的作参考! C++版本 class Solution { pub

(LeetCode)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. It doesn't matter

[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] 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 python

class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ if len(nums) <= 0: return 0 k=0 for i in nums: if i != val: nums[k]=i k+=1 return k

leetcode Remove Element(easy) /java

和上一道题思路差不多. import java.io.*; import java.util.*; public class Solution { public static int removeElement(int[] nums, int val) { int r=0; int len=nums.length; int i=0,j=0; if(len==0) return 0; int c=0; for(i=0;i<len;i++) { if(val!=nums[i]) { nums[j]=