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,10 +42,12 @@
#### **题目代码** #### **题目代码**
Java Code:
```java ```java
class Solution { class Solution {
public boolean search(int[] nums, int target) { public boolean search(int[] nums, int target) {
int left = 0; int left = 0;
int right = nums.length - 1; int right = nums.length - 1;
while (left <= right) { while (left <= right) {
int mid = left+((right-left)>>1); int mid = left+((right-left)>>1);
@ -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
}
```