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.

题目的要求是这个,最初的想法非常简单,首先用一个变量length记录下数组A的长度,其次对A中的元素开始遍历比较,如果A中的元素和ele相同,则将length减1

        int length = A.length;
        for(int i = 0;i<A.length;i++){
            if(A[i] == elem){
                length = length - 1;
            }
        }
        return length;        

提交之后出现了错误,我觉得最主要的原因可能是没有改变原来的数组,只是针对数组的长度变化。

修改之后:

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

提交成功,之后看了网上看了别人的方法,大部分人都是如果遇到elem,就将它调整到数组的后面去,

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];
                i--;
                length--;
            }
        }
        return length;
    }
}

觉得它i和length处理那里很好,拿来借鉴。

时间: 2024-09-09 21:24:27

leetcode之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 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 思路 好吧,感觉这个题目出的不是很好,按照

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[