Remove Element -- leetcode

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上执行时间为36ms。

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

算法虽然是O(n),但缺点是数据移动量大。

只要存在一个和elem相同的元素,那意味其后的元素都需要移动。

算法二:首尾双指针向中间移动

此算法可以大量减少移动操作。当遇到和elem相同的元素时,从数组尾部取数来填充。

此算法在leetcode上执行时间为20ms。 比算法一用时明显减少。

class Solution {
public:
    int removeElement(int A[], int n, int elem) {
        int num = 0;
        while (num < n) {
                if (A[num] == elem)
                        A[num] = A[--n];
                else
                        ++num;
        }
        return num;
    }
};
时间: 2024-08-29 20:42:05

Remove Element -- leetcode的相关文章

Remove Element leetcode java

题目: 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 duplicate sorted array是一样的...还是双指针,一个帮

[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 Duplicates from Sorted Array &amp;&amp; Remove Element

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A =

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

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 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 change

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

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. Solution: class Solution { public: int removeElement(int

[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] 027. Remove Element (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 027. Remove Element (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-element/ 代码(github):https://github.com/illuz/leetcode 题意: 删除一个数组里值为 elem 的所有数. 分析: 用