All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0027_remove_element.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0001_0100.s0027_remove_element;

// #Easy #Array #Two_Pointers #2022_06_14_Time_0_ms_(100.00%)_Space_42.5_MB_(46.77%)

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int len = nums.length;
        int j = len - 1;
        int occurTimes = 0;
        for (int i = 0; i < len; i++) {
            if (nums[i] == val) {
                occurTimes++;
                if (j == i) {
                    return len - occurTimes;
                }
                while (nums[j] == val) {
                    j--;
                    occurTimes++;
                    if (j == i) {
                        return len - occurTimes;
                    }
                }
                nums[i] = nums[j];
                j--;
            }
            if (i == j) {
                return len - occurTimes;
            }
        }
        return len - occurTimes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy