Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit af85cd9

Browse files
author
Sandy
authored
Merge pull request #6 from openset/develop
Add: remove_element
2 parents 8c94d7b + 0a3486a commit af85cd9

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

‎solution/remove-element/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 27. Remove Element
2+
3+
Given an array nums and a value val, remove all instances of that value `in-place` and return the new length.
4+
5+
Do not allocate extra space for another array, you must do this by `modifying the input array` `in-place` with O(1) extra memory.
6+
7+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
8+
9+
**Example 1:**
10+
```
11+
Given nums = [3,2,2,3], val = 3,
12+
13+
Your function should return length = 2, with the first two elements of nums being 2.
14+
15+
It doesn't matter what you leave beyond the returned length.
16+
```
17+
**Example 2:**
18+
19+
```
20+
Given nums = [0,1,2,2,3,0,4,2], val = 2,
21+
22+
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
23+
24+
Note that the order of those five elements can be arbitrary.
25+
26+
It doesn't matter what values are set beyond the returned length.
27+
```
28+
**Clarification:**
29+
30+
Confused why the returned value is an integer but your answer is an array?
31+
32+
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.
33+
34+
Internally you can think of this:
35+
36+
```
37+
// nums is passed in by reference. (i.e., without making a copy)
38+
int len = removeElement(nums, val);
39+
40+
// any modification to nums in your function would be known by the caller.
41+
// using the length returned by your function, it prints the first len elements.
42+
for (int i = 0; i < len; i++) {
43+
print(nums[i]);
44+
}
45+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package remove_element
2+
3+
func removeElement(nums []int, val int) int {
4+
l := 0
5+
for _, v := range nums {
6+
if v != val {
7+
nums[l] = v
8+
l++
9+
}
10+
}
11+
return l
12+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package remove_element
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type caseType struct {
8+
input []int
9+
val int
10+
expected []int
11+
}
12+
13+
func TestRemoveElement(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: []int{3, 2, 2, 3},
17+
val: 3,
18+
expected: []int{2, 2},
19+
},
20+
{
21+
input: []int{0, 1, 2, 2, 3, 0, 4, 2},
22+
val: 2,
23+
expected: []int{0, 1, 3, 0, 4},
24+
},
25+
{
26+
input: []int{1, 2, 3, 4, 5},
27+
val: 6,
28+
expected: []int{1, 2, 3, 4, 5},
29+
},
30+
{
31+
input: []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4},
32+
val: 4,
33+
expected: []int{1, 2, 2, 3, 3, 3},
34+
},
35+
{
36+
input: nil,
37+
val: 1,
38+
expected: nil,
39+
},
40+
}
41+
42+
for _, tc := range tests {
43+
nums := make([]int, len(tc.input))
44+
copy(nums, tc.input)
45+
l := removeElement(nums, tc.val)
46+
output := nums[:l]
47+
if l != len(tc.expected) {
48+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
49+
}
50+
for k, v := range tc.expected {
51+
if output[k] != v {
52+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /