add lc81 code in go

This commit is contained in:
lincongcong 2021-07-26 23:59:59 +08:00
parent a1afdf4e26
commit 3096e19a2a

View File

@ -42,6 +42,8 @@
#### **题目代码**
Java Code:
```java
class Solution {
public boolean search(int[] nums, int target) {
@ -76,4 +78,38 @@ class Solution {
}
```
Go Code:
```go
func search(nums []int, target int) bool {
left := 0
right := len(nums) - 1
for (left <= right) {
mid := left+((right-left)>>1)
if (nums[mid] == target) {
return true
}
if (nums[mid] == nums[left]) {
left++
continue
}
if (nums[mid] > nums[left]) {
if (nums[mid] > target && target >= nums[left]) {
right = mid - 1
} else if (target > nums[mid] || target < nums[left]) {
left = mid + 1
}
}else if (nums[mid] < nums[left]) {
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1
} else if (target < nums[mid] || target > nums[right]) {
right = mid - 1
}
}
}
return false
}
```