Can anyone tell me what this problem means


  • 2
    M
    public int removeElement(int[] A, int elem) {
    	int oldLength = A.length;
    	int newLength = oldLength;
    	for (int i = 0;i<oldLength;i++) {
    		if (A[i] == elem)
    			newLength--;
    	}
    	return newLength;
    }

  • 4
    S

    You should count the newLength, and also make the first newLength elements in the array are the values which do not need to be removed.

    For example, A=[0,2,3,2,5] elem=2, you should return 3 and also make the original A array like [0,3,5,2,2]. However, the problem description has announced that order is ok, so your A might be [5,0,3,2,2] or whatever.


  • 0
    This post is deleted!

  • 10

    "remove all instances of that value in place and return the new length". notice that you should remove the elem in int[] A but not only count and calculate the newLength. in your code, you keep the original array A[], not changing it to a new array

    public int removeElement(int[] A, int elem) {
        int count = 0;
        for (int i = 0; i < A.length; ++i) {
            if (A[i] == elem) {
                ++count;
            } else if(count > 0) {
                A[i - count] = A[i];
            }
        }
        return A.length - count;
    }
    

    this is my method, in the else statement, replace the element in A[] with the element after it.


  • 0
    V
    This post is deleted!

  • 0
    E

    Use a variable to store the boundary between two sets of elements. No extra of copy operation is needed.

    class Solution {
    public:
        int removeElement(int A[], int n, int elem) {
            
            int boundary = n;
            
            for (int i = 0; i < boundary; ++i) {
                if (A[i] == elem) {
                    while (--boundary > i && A[boundary] == elem)
                        ;
                    
                    if (boundary == i)
                        return boundary;
                    else
                        std::swap(A[i], A[boundary]);
                }
            }
            
            return boundary;
        }
    };

  • 0
    S

    Thanks for your sharing.

    However, answers you've written must describe your code with more detail content. Please read the FAQ (http://oj.leetcode.com/discuss/faq) for more info.


  • 0
    L

    i think when you submit ,the sever doest not only test the RETURN Length,but also test A[]


  • 0
    W

    traverse the array, when we found A[i]==elem, assign the last number of array(A[length-1]) to A[i], then i--,length--, so at last length will be the answer

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

  • 0
    S

    Hi @wbqhust, appreciate your answer. Even though your solution is super elegant, it would be better to word your idea.


  • 0
    A

    i have no idea what your mean, can u expalin it in more details?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.