mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-28 06:48:53 +00:00
add lc81 code in go
This commit is contained in:
parent
a1afdf4e26
commit
3096e19a2a
@ -42,10 +42,12 @@
|
||||
|
||||
#### **题目代码**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public boolean search(int[] nums, int target) {
|
||||
int left = 0;
|
||||
int left = 0;
|
||||
int right = nums.length - 1;
|
||||
while (left <= right) {
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user