[leetcode 50]remove element

1 题目

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.

Hide Tags

Array Two Pointers

2 思路

好吧,感觉这个题目出的不是很好,按照题目要求,要改变A数组的长度,我一想,数组长度怎么改。。,看了答案,原来只是把和elem相同的元素,取另外一个元素覆盖这个相同的元素即可。也就是结果应该是前面startPosition(最终返回的结果)个元素里面没有elem,且是A[]数组里面排出elem剩下的元素。

3 代码

    public int removeElement(int[] A, int elem) {
        int startPosition = 0;
        for (int i = 0; i < A.length; i++) {
            if (A[i] != elem) {
                A[startPosition++] = A[i];
            }
        }
        return startPosition;
    }
时间: 2024-10-07 18:07:16

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

[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 的所有数. 分析: 用

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 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 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. 解法一:使用vector记录每个elem的位置,介于这些位置之间的元素就可以前移相应

LeetCode 027 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. 代码如下: class Solution { public: int re

leetcode 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. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public:

leetcode 【 Remove Element 】python 实现

题目: 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. 代码 : oj测试通过 Runtime: 43 ms 1 class Solution: 2 # @par

Leetcode 27 Remove Element STL

和remove zero类似的方法完成该题 1 class Solution { 2 public: 3 int removeElement(vector<int>& nums, int val) { 4 vector<int>::size_type j = 0; 5 for(vector<int>::size_type i = 0; i < nums.size(); ++i){ 6 if(nums[i] != val) nums[j++] = nums[