Skip to content

Commit d6bd5d8

Browse files
authored
remove element (#9)
1 parent 899b5f5 commit d6bd5d8

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ continually updating 😃.
1111

1212
### Array
1313
* [1. Two Sum](./src/0001_two_sum/twosum.go)   *`lookup table;`*  *`hash table`*
14+
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
1415
* [167. Two Sum II - Input array is sorted](./src/0167_two_sum2/two_sum2.go)   *`double index;`*  *`binary search`*
1516
* [209. Minimum Size Subarray Sum](./src/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)   *`sliding window`*
1617
* [283. Move Zeroes(solution1)](./src/0283_move_zeroes/move_zeroes.go)   *`sliding window`*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
27. Remove Element
3+
https://leetcode.com/problems/remove-element/
4+
5+
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
6+
7+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
8+
9+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
10+
*/
11+
// time: 2018-12-20
12+
13+
package removeelement
14+
15+
// two pointers
16+
// time complexity: O(n)
17+
// space complexity: O(1)
18+
func removeElement(nums []int, val int) int {
19+
x := 0
20+
for j := 0; j < len(nums); j++ {
21+
if nums[j] != val {
22+
if x != j {
23+
nums[x] = nums[j]
24+
}
25+
x++
26+
}
27+
}
28+
return x
29+
}
30+
31+
/*
32+
func removeElement1(nums []int, val int) int {
33+
var (
34+
l int
35+
r = len(nums)
36+
)
37+
for l < r {
38+
if nums[l] == val {
39+
r--
40+
nums[l] = nums[r]
41+
} else {
42+
l++
43+
}
44+
}
45+
return r
46+
}
47+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package removeelement
2+
3+
import "testing"
4+
5+
func TestRemoveElement(t *testing.T) {
6+
nums := []int{0, 1, 2, 2, 3, 0, 4, 2}
7+
val := 2
8+
expected := 5
9+
10+
if res := removeElement(nums, val); res != expected {
11+
t.Errorf("expected %d, got %d", expected, res)
12+
}
13+
}

src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
1010
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
1111
|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|
12+
|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|
1213
|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|
1314
|0034|[ Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|
1415
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|

0 commit comments

Comments
 (0)