-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveElement.java
More file actions
36 lines (32 loc) · 838 Bytes
/
RemoveElement.java
File metadata and controls
36 lines (32 loc) · 838 Bytes
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
package com.leetcode;
/**
* Author: yhl
* DateTime: 2019/11/4 17:21
* Description: write some description
*/
public class RemoveElement {
public static void main(String[] args) {
int[] arr = {3, 2, 2, 2, 3, 4, 4, 4};
final int i = new RemoveElement().removeElement1(arr, 2);
System.out.println(i);
}
public int removeElement(int[] nums, int val) {
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[index] = nums[i];
index++;
}
}
return index;
}
public int removeElement1(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
i++;
}
}
return i;
}
}