# Go brush 1.0 every Monday

Time：2021-9-25 The picture was taken on January 2, 2021, Zhoushan Dongji island.

As I said, I will brush the questions according to the labels. Brush four to five questions for each category and divide it into two articles, the first basic question and the second slightly modified question. Let’s start with two points.

### Leetcode 704 This is the most typical binary basic problem. It is simple to find the target value from an ordered set without considering the problem of element repetition.

``````func search(nums []int, target int) int {
left, right := 0, len(nums)-1
var mid int
for left <= right {
// mid = (left + right) / 2
//Get median
mid = left + ((right - left) >> 1)
//If the median value is equal to the target value, it is found and returned directly
if nums[mid] == target {
return mid
//If the value of the current median > the target value, it means that the value can only exist in the left range of the median,
//And does not include the median
} else if nums[mid] > target {
right = mid - 1
//Otherwise, the current median value is less than the target value, indicating that the value can only exist in the right range of the median,
//And does not include the median
} else {
left = mid + 1
}
}
return -1
}``````

It should be noted that when calculating the median, if only the simple mid = (left + right) / 2, the addition will cause overflow when the number is too large. Therefore, the bit operation is directly used here.

The above code is not very rigorous. For example, there is a filter at the beginning.

``````if len(nums) == 0 {
return -1
}``````

Another point worth discussing is left < = right. Many people will struggle whether it is < or < =. This is because different understanding angles will lead to similar solutions and differentiated codes. As long as you can understand the boundary problem, it doesn’t matter what you write. But I still feel that good code is not just how concise and advanced skills, but easy to understand.

Left and right are the subscripts of the array. They represent the range of the current search. In the program, the judgment result is used to update whether the next interval to be found is reduced to the left or right.

When will it end?

First, the program has found the result. Of course, it runs directly.

Second, the result is not found, and the interval is continuously reduced. Finally, the interval has been reduced to 0. There is no interval value, and it is over.

Now you see why < = is needed above. First of all, left must not be greater than right. For example, [5,4] this can be a normal interval. As for =, the reason is also very simple. There is also a public 5 in [5,5]. If it is omitted, it will lead to program error.

### Leetcode 34 This problem is a little more difficult than just now. It is also to find the location of the target value. We need to return two values. One value is the first location where the target value appears and the last location where the target value appears.

Can we still use it?

Change can use!

In essence, the judgment on the narrowing of the left and right ranges is the same. The only difference is that before we found the target value, we returned the result, but now we can’t. We have to further confirm whether the target value corresponds to the first and last positions.

``````func searchRange(nums []int, target int) (res []int) {
return append([]int{}, findFirstIndex(nums, target), findLastIndex(nums, target))
}

//Find element first
func findFirstIndex(nums []int, target int) int {
var mid int
left, right := 0, len(nums)-1
for left <= right {
//If the digital conference causes overflow
// mid = (left + right)/2
//Using bit operations
mid = left + ((right - left) >> 1)
//If the current median value is larger than the target value, it means that the target value may only have the left range of the median (excluding the median)
if nums[mid] > target {
right = mid - 1
//If the current median value is smaller than the target value, the target value may only have the right range of the median
} else if nums[mid] < target {
left = mid + 1
}Else {// indicates that the median value is equal to the target value, but it is not sure that it is the first of the same target value
//In the case of equality, if the current median index is 0, or the value of the previous index position of the current median is not equal to the target value, it must be it, and the program ends
if mid == 0 || (nums[mid-1] != target) {
return mid
}Else {// otherwise, it must be on the left. Squeeze again in the left section
right = mid - 1
}
}
}
//If none is found, return - 1
return -1
}

//Find the last element
func findLastIndex(nums []int, target int) int {
var mid int
left, right := 0, len(nums)-1
for left <= right {
mid = left + ((right - left) >> 1)
if nums[mid] > target {
right = mid - 1
} else if nums[mid] < target {
left = mid + 1
}Else {// indicates that the median value is equal to the target value at this time, but it is not sure that it is the last one of the same target value
//In the case of equality, if the current median index is in the last position, or the value of the next index position of the current median is not equal to the target value, it must be it, and the program ends
if mid == len(nums)-1 || nums[mid+1] != target {
return mid
}Else {// otherwise, it must be squeezed from the right to the right section
left = mid + 1
}
}
}
//If none is found, return - 1
return -1
}``````

That’s all for this issue. Do you have a different solution? Then please leave a message and tell me. The code is placed in:github.com/wuqinqiang/LeetCode-Go-… 