Thinking process

http://www.programcreek.com/2014/04/leetcode-remove-element-java/

similar to LC 26

move right pointer, if it is not equal to the value, set the value at left pointer to that value.

public class Solution {
    public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = 0;

        while (right < nums.length) {
            if (nums[right] != val) {
                nums[left] = nums[right];
                left++;
            }
            right++;
        }

        return left;
    }
}

results matching ""

    No results matching ""