Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

Spring Recruit punch card day 17 chapter 20.

Study like the seedlings of spring, see its increase, day has a director; Drop out of school such as a whetstone, see its loss, loss.

There are so many activities in digging gold. This month, I decided to use GO to brush the questions every day, on the one hand, to improve the algorithm level, on the other hand, to settle the learning of GO language.

Let’s GO!

Topic describes

Given an array of nums and a value of val, you remove all elements equal to val in place and return the new length of the array.

Instead of using extra array space, you must only use O(1) extra space and modify the input array in place.

The order of elements can be changed. You don’t need to worry about the element after the new length in the array.

 

Description:

Why is the return value an integer, but the output answer is an array?

Note that the input array is passed “by reference,” which means that modifying the input array in a function is visible to the caller.

You can imagine the internal operation as follows:

// Nums is passed by reference. That is, no copies of the arguments are made
int len = removeElement(nums, val);

// Modifying the input array in a function is visible to the caller.
// Depending on the length returned by your function, it prints out all elements in the array within that length.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}
Copy the code

 

The sample

Example 1:

Enter nums = [3,2,2,3], val = 3

Output: 2, nums = [2,2]

Explanation: The function should return a new length of 2, and the first two elements in nums are both 2. You don’t need to worry about the element after the new length in the array. For example, the function returns a new length of 2, and nums = [2,2,3,3] or nums = [2,2,0,0] will also be considered the correct answer.

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2

5, nums = [0,1,4,0,3]

Explanation: The function should return a new length of 5, and the first five elements in nums are 0, 1, 3, 0, 4. Notice that these five elements can be in any order. You don’t need to worry about the element after the new length in the array.

Tip:

0 <= nums.length <= 100

0 <= nums[i] <= 50

0 <= val <= 100

Their thinking

  1. The first time to see the title to see the clouds in the fog, read again carefully feel simple and unusual ah, do not be in the titleTo do reference passing, don't bother with value references
  2. Remember that in go, a slice is a reference type, not a value type
  3. We simply iterate over the input slice, taking only the elements in the slice whose value differs from val and assigning them to the slice
  4. Take care to manage the index of the slice, because there are bound to be some that do not meet the criteria during the traversal. Let’s start the index at 0 and increment it by 1. (of course we can use x++, evaluate first and then assign, to optimize the code)
  5. All right, let’s roll out the code

AC code

func removeElement(nums []int, val int) int {
    ret := 0
    for i:=0; i<len(nums); {ifnums[i] ! = val { nums[ret] = nums[i] ret++ } i++ }return ret
}
Copy the code

The results

conclusion

Brush the problem for a while, always brush the problem straight wow.

This one performed particularly well, beating 100% of users in time and 99.96% in memory consumption.

Go language Slice really easy to use: Go Slice Slice and actual combat

sources

Source: LeetCode

Link: leetcode-cn.com/problems/re…

Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The last

Thanks for reading and welcome to like, favorites,coin(attention)!!