share my java o(n) solution


  • 0
    W
    public int removeElement(int[] nums, int val) {
        int start = 0;
        int index = 0;
        int len = nums.length;
        while(index < len) {
            if(nums[index] == val) {
                index++;
            } else {
                if(index != start) {
                    int tmp = nums[index];
                    nums[index] = nums[start];
                    nums[start] = tmp;
                }
                start++; index++;
            }
        }
        return start;
    }

Log in to reply
 

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