27. Remove Element

来源:互联网 发布:弦理论 知乎 编辑:程序博客网 时间:2024/04/29 15:10

Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

46% 

nums[l++] = nums[I] l++提升速度

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


0 0