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 53cd4e9

Browse files
Go: 27
1 parent 1a1caba commit 53cd4e9

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
5050
|-----|----------------|:---------------:|:--------:|:-------------:
5151
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_14.java) & Python|Easy| Introduction to String
5252
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_26.java) & Python|Easy| Conclusion
53-
|27|[Remove Element](https://leetcode.com/problems/implement-strstr/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_27.java) & Python|Easy| Two-Pointer Technique
53+
|27|[Remove Element](https://leetcode.com/problems/remove-element/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_27.java) & Python & [Go](https://github.com/guobinhit/myleetcode/blob/master/codes/go/leetcodes/learn/array_and_string/_27.go)|Easy| Two-Pointer Technique
5454
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_28.java) & Python|Easy| Introduction to String
5555
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_54.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_54.py)|Medium| Introduction to 2D Array
5656
|66|[Plus One](https://leetcode.com/problems/plus-one/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_66.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_66.py)|Easy| Introduction to Array
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package array_and_string
2+
3+
/**
4+
* 27. Remove Element
5+
* <p>
6+
* Given an array nums and a value val, remove all instances of that value in-place and return the new length.
7+
* <p>
8+
* Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
9+
* <p>
10+
* The order of elements can be changed. It doesn't matter what you leave beyond the new length.
11+
* <p>
12+
* Example 1:
13+
* <p>
14+
* Given nums = [3,2,2,3], val = 3,
15+
* <p>
16+
* Your function should return length = 2, with the first two elements of nums being 2.
17+
* <p>
18+
* It doesn't matter what you leave beyond the returned length.
19+
* <p>
20+
* Example 2:
21+
* <p>
22+
* Given nums = [0,1,2,2,3,0,4,2], val = 2,
23+
* <p>
24+
* Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
25+
* <p>
26+
* Note that the order of those five elements can be arbitrary.
27+
* <p>
28+
* It doesn't matter what values are set beyond the returned length.
29+
* <p>
30+
* Clarification:
31+
* <p>
32+
* Confused why the returned value is an integer but your answer is an array?
33+
* <p>
34+
* 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.
35+
* <p>
36+
* Internally you can think of this:
37+
* <p>
38+
* // nums is passed in by reference. (i.e., without making a copy)
39+
* int len = removeElement(nums, val);
40+
* <p>
41+
* // any modification to nums in your function would be known by the caller.
42+
* // using the length returned by your function, it prints the first len elements.
43+
* for (int i = 0; i < len; i++) {
44+
* print(nums[i]);
45+
* }
46+
*/
47+
48+
func removeElement(nums []int, val int) int {
49+
aimLength := 0
50+
for _, n := range nums {
51+
if n != val {
52+
nums[aimLength] = n
53+
aimLength++
54+
}
55+
}
56+
return aimLength
57+
}

0 commit comments

Comments
(0)

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