Skip to content

Commit bbe96a3

Browse files
committed
Add Solution.java for 0027.Remove Element
1 parent b86db97 commit bbe96a3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int removeElement(int[] nums, int val) {
3+
int res = 0, length = nums.length;
4+
for (int i = 0; i < length; i ++) {
5+
if (nums[i] != val) {
6+
nums[res] = nums[i];
7+
res ++;
8+
}
9+
}
10+
return res;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int removeElement(int[] nums, int val) {
3+
int limit = 0, length = nums.length;
4+
for (int i = 0, j = 0; i < length - limit; i ++) {
5+
while (j < length && nums[j ++] == val) {
6+
limit ++;
7+
}
8+
if (i + limit < length) {
9+
nums[i] = nums[i + limit];
10+
}
11+
}
12+
return length - limit;
13+
}
14+
}

0 commit comments

Comments
 (0)