1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public class RemoveElement { public int removeElement(int[] nums, int val) { if (nums == null || nums.length == 0) { return 0; } int crt = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != val) { nums[crt++] = nums[i]; } } return crt; } @Test public void test() { int[] nums = new int[]{1, 1}; int result = removeElement(nums, 1); } }
|