首页 > 代码库 > 27. Remove Element

27. Remove Element

https://leetcode.com/problems/remove-element/#/description

 

public class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int len = nums.length, index = 0;
        for (int num : nums) {
            if (num != val) {
                nums[index++] = num;
                
            } else {
                len--;
            }
        }
        return len;
    }
}

  

27. Remove Element