mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 04:46:17 +00:00
代码重构 【Github Actions】
This commit is contained in:
parent
94d9b4c70f
commit
d0abf3e1d8
@ -96,7 +96,7 @@ func search(nums []int, target int) bool {
|
|||||||
right = mid - 1
|
right = mid - 1
|
||||||
} else if (target > nums[mid] || target < nums[left]) {
|
} else if (target > nums[mid] || target < nums[left]) {
|
||||||
left = mid + 1
|
left = mid + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
}else if (nums[mid] < nums[left]) {
|
}else if (nums[mid] < nums[left]) {
|
||||||
if (nums[mid] < target && target <= nums[right]) {
|
if (nums[mid] < target && target <= nums[right]) {
|
||||||
@ -104,7 +104,7 @@ func search(nums []int, target int) bool {
|
|||||||
} else if (target < nums[mid] || target > nums[right]) {
|
} else if (target < nums[mid] || target > nums[right]) {
|
||||||
right = mid - 1
|
right = mid - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -113,10 +113,10 @@ func findMin(nums []int) int {
|
|||||||
right := len(nums) - 1
|
right := len(nums) - 1
|
||||||
|
|
||||||
for (left < right) {
|
for (left < right) {
|
||||||
|
|
||||||
if (nums[left] < nums[right]) {
|
if (nums[left] < nums[right]) {
|
||||||
return nums[left]
|
return nums[left]
|
||||||
}
|
}
|
||||||
mid := left + ((right - left) >> 1)
|
mid := left + ((right - left) >> 1)
|
||||||
if (nums[left] > nums[mid]) {
|
if (nums[left] > nums[mid]) {
|
||||||
right = mid
|
right = mid
|
||||||
|
@ -163,5 +163,5 @@ func search(nums []int, target int) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -203,13 +203,13 @@ func lowerBound(nums []int, target int) int {
|
|||||||
//计算上边界
|
//计算上边界
|
||||||
func upperBound(nums []int, target int) int {
|
func upperBound(nums []int, target int) int {
|
||||||
left, right := 0, len(nums) - 1
|
left, right := 0, len(nums) - 1
|
||||||
for (left <= right) {
|
for (left <= right) {
|
||||||
mid := left + ((right - left) >> 1)
|
mid := left + ((right - left) >> 1)
|
||||||
if (target >= nums[mid]) {
|
if (target >= nums[mid]) {
|
||||||
left = mid + 1
|
left = mid + 1
|
||||||
}else if (target < nums[mid]) {
|
}else if (target < nums[mid]) {
|
||||||
right = mid - 1
|
right = mid - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return right
|
return right
|
||||||
}
|
}
|
||||||
|
@ -140,7 +140,6 @@ func binarySearch(nums []int, target, left, right int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
二分查找的思路及代码已经理解了,那么我们来看一下实现时容易出错的地方
|
二分查找的思路及代码已经理解了,那么我们来看一下实现时容易出错的地方
|
||||||
|
|
||||||
1.计算 mid 时 ,不能使用 (left + right )/ 2,否则有可能会导致溢出
|
1.计算 mid 时 ,不能使用 (left + right )/ 2,否则有可能会导致溢出
|
||||||
@ -157,7 +156,7 @@ Java Code:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
public static int binarySearch(int[] nums,int target,int left, int right) {
|
public static int binarySearch(int[] nums,int target,int left, int right) {
|
||||||
|
|
||||||
if (left <= right) {
|
if (left <= right) {
|
||||||
int mid = left + ((right - left) >> 1);
|
int mid = left + ((right - left) >> 1);
|
||||||
if (nums[mid] == target) {
|
if (nums[mid] == target) {
|
||||||
|
@ -87,6 +87,7 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func searchMatrix(matrix [][]int, target int) bool {
|
func searchMatrix(matrix [][]int, target int) bool {
|
||||||
if len(matrix) == 0 {
|
if len(matrix) == 0 {
|
||||||
|
@ -62,6 +62,7 @@ public static int lowBoundnum(int[] nums,int target,int left, int right) {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go Code:
|
Go Code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func lowBoundnum(nums []int, target, left, right int) int {
|
func lowBoundnum(nums []int, target, left, right int) int {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user