-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS27.java
37 lines (34 loc) · 1.05 KB
/
S27.java
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
28
29
30
31
32
33
34
35
36
37
/**
* #27. Remove Element
* https://leetcode-cn.com/problems/remove-element/
* 思路:通过变量i 重排数组,通过 numbers[j] == val 跳过指定元素
* @author xiaobin
*/
public class S27 {
public static int removeElement(int[] numbers, int val) {
// int i = 0;
// int n = nums.length;
// while (i < n) {
// if(nums[i] == val) {
// nums[i] = nums[n - 1];
// n--;
// } else {
// i++;
// }
// }
// return i;
// 通过变量i 重排数组,通过 nums[j] == val 跳过指定元素,获取移除元素后的新数组
int i = 0;
for (int j = 0; j < numbers.length; j++) {
if (numbers[j] == val) { continue; }
numbers[i] = numbers[j];
i++;
}
return i;
}
public static void main(String[] args) {
int[] numbers = {0,1,2,2,3,0,4,2};
int newLength = S27.removeElement(numbers, 2);
System.out.println("newLength=" + newLength);
}
}