第18题 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.

Solution1:

public class Solution {
    public int removeElement(int[] A, int elem) {
        int length=0, ptr=0;
        for(int i=0; i<A.length;i++){
            if(A[i]!=elem){
                A[ptr]=A[i];
                ptr++;
                length++;
            }
        }
        return length;
    }
}

Solution2:不要的用数组尾的数据填充,改变数组长度即可。

public class Solution {
    public int removeElement(int[] A, int elem) {
        int length=A.length;
        for(int i=0; i<length;i++){
            if(A[i]==elem){
                A[i]=A[length-1];
                length--;
                i--;
            }
        }
        return length;
    }
}
时间: 2024-10-06 15:52:27

第18题 Remove Element的相关文章

leetcode第25题--Remove Element

problem: 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

#include<stdio.h> #include<stdlib.h> int removeElement(int A[], int n, int elem) { if(n==0) return 0; int len=n; for(int i=0,pos=0;i<n;i++) { if(A[i]==elem) len--; else A[pos++]=A[i]; } return len; } int main() { int n; while(scanf("%d

leetcode 刷题之路 86 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. 给定一个数组和一个数字,删除数组中相同的数字并返回数组的新长度. 这道题解法很多,第一种,每删除一个数字都将后面的

27. Remove Element【easy】

27. Remove Element[easy] 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 ch

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

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

No.27 Remove Element

No.27 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. Tags: Array Two Pointers 移除数组中所有的给定数

乘风破浪:LeetCode真题_027_Remove Element

乘风破浪:LeetCode真题_027_Remove Element 一.前言 这次是从数组中找到一个元素,然后移除该元素的所有结果,并且返回长度. 二.Remove Element 2.1 问题 2.2 分析与解决     这个题和上一题是非常相似的,只不过这次是从数组中找到给定的元素,并且删除该元素,同时返回剩余数组的长度,超过长度的元素不用管,存不存在都可以.于是我们想到了和上次一样的方法,用一个指针指向开始,一个指向结尾,开始的向后移动,如果遇到需要删除的元素,则用最后的元素替代,最后的