-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveElement.java
More file actions
29 lines (27 loc) · 649 Bytes
/
RemoveElement.java
File metadata and controls
29 lines (27 loc) · 649 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
package leetcode.array;
/**
* @Author: ChangXuan
* @Decription: 移除元素 https://leetcode-cn.com/problems/remove-element/
* @Date: 22:39 2020/8/22
**/
public class RemoveElement {
/**
* 快慢指针
* @param nums 数组
* @param val 被移除元素
* @return 新数组长度
*/
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length == 0) return 0;
int p = 0;
int q = 0;
while(q < nums.length){
if(nums[q] != val){
nums[p] = nums[q];
p++;
}
q++;
}
return p;
}
}