27. Remove Element

Total Accepted: 101318
Total Submissions: 307664
Difficulty: Easy

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.

Java:

1
2
3
4
5
6
7
8
9
10
public class Solution {
    public int removeElement(int[] nums, int val) {
        int len=nums.length;
        int res=0;
        for(int i=0;i<len;++i)
            if(nums[i]!=val)
                nums[res++]=nums[i];
        return res;
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。