Skip to content

Commit 1863f5f

Browse files
committed
27. Remove Element
1 parent 4f7fb97 commit 1863f5f

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/leetcode/_27_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._27_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[] ints = {0,1,2,2,3,0,4,2};
10+
// int[] ints = {3, 2, 2, 3};
11+
int i = solution.removeElement(ints, 2);
12+
System.out.println(i);
13+
}
14+
}
15+

src/leetcode/_27_/Solution.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._27_;
2+
3+
class Solution {
4+
public int removeElement(int[] nums, int val) {
5+
int seat = 0;
6+
for (int i = 0; i < nums.length; i++) {
7+
if (nums[i] != val) {
8+
nums[seat++] = nums[i];
9+
}
10+
}
11+
return seat;
12+
}
13+
}

src/leetcode/_27_/solution.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
### [27\. Remove ElementCopy for Markdown](https://leetcode.com/problems/remove-element/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given an array _nums_ and a value _val_, remove all instances of that value and return the new length.
7+
8+
Do not allocate extra space for another array, you must do this by **modifying the input array** with O(1) extra memory.
9+
10+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
11+
12+
**Example 1:**
13+
14+
```
15+
Given nums = [3,2,2,3], val = 3,
16+
17+
Your function should return length = 2, with the first two elements of nums being 2.
18+
19+
It doesn't matter what you leave beyond the returned length.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Given nums = [0,1,2,2,3,0,4,2], val = 2,
26+
27+
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
28+
29+
Note that the order of those five elements can be arbitrary.
30+
31+
It doesn't matter what values are set beyond the returned length.```
32+
33+
**Clarification:**
34+
35+
Confused why the returned value is an integer but your answer is an array?
36+
37+
Note that the input array is passed in by **reference**, which means modification to the input array will be known to the caller as well.
38+
39+
Internally you can think of this:
40+
41+
```
42+
// nums is passed in by reference. (i.e., without making a copy)
43+
int len = removeElement(nums, val);
44+
45+
// any modification to nums in your function would be known by the caller.
46+
// using the length returned by your function, it prints the first len elements.
47+
for (int i = 0; i < len; i++) {
48+
    print(nums[i]);
49+
}```
50+
51+
52+
#### Solution
53+
54+
Language: **Java**
55+
56+
```java
57+
class Solution {
58+
   public int removeElement(int[] nums, int val) {
59+
       int seat = 0;
60+
       for (int i = 0; i < nums.length; i++) {
61+
           if (nums[i] != val) {
62+
               nums[seat++] = nums[i];
63+
          }
64+
      }
65+
       return seat;
66+
  }
67+
}
68+
```
69+
![](https://ws3.sinaimg.cn/large/006tKfTcgy1g0z3os9l92j30ue0t0djd.jpg)

0 commit comments

Comments
 (0)