My java accepted solution.


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

Log in to reply
 

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