mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-01 00:08:53 +00:00
add lc81 code in go
This commit is contained in:
parent
a1afdf4e26
commit
3096e19a2a
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user