Leetcode 27 Remove-Element

HowieLee59發表於2018-10-24

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

這個題的題意和上一個題貌似是一樣的,使用了同樣的方法。

1)

public class Solution {
    public int removeElement(int[] nums, int elem) {
        int i = 0;
        for(int j = 0 ; j < nums.length ; j++){
            if(nums[j] != elem){
                nums[i] = nums[j];
                i++;
            }
        }
        return i;
    }
}

 

相關文章