-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package problem154 | ||
|
||
func findMin(nums []int) int { | ||
left, right := 0, len(nums)-1 | ||
var mid int | ||
for left < right { | ||
if nums[left] < nums[right] { | ||
return nums[left] | ||
} | ||
mid = (left + right) / 2 | ||
// if nums[mid] > nums[left] { | ||
// left = mid + 1 | ||
// } else if nums[mid] < nums[right] { | ||
// right = mid | ||
// } | ||
// 上一种写法并不合理。*mid只需要比*right大。不需要比*left大,就可以说明mid落在左边 | ||
// if nums[mid] < nums[left] { | ||
if nums[mid] > nums[right] { | ||
left = mid + 1 | ||
} else if nums[mid] < nums[right] { | ||
right = mid | ||
} else { | ||
// nums[left]<=nums[mid]<=nums[right] | ||
// 三者相等 | ||
left++ | ||
} | ||
} | ||
return nums[left] | ||
} |