Skip to content

Commit 58f23c3

Browse files
committed
find_minimum_in_rotated_sorted_array
1 parent 682eb5f commit 58f23c3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
136136
#### [150. evaluate reverse polish notation](https://github.com/hitzzc/go-leetcode/tree/master/evaluate_reverse_polish_notation)
137137
#### [151. reverse words in a string](https://github.com/hitzzc/go-leetcode/tree/master/reverse_words_in_a_string)
138138
#### [152. maximum product subarray](https://github.com/hitzzc/go-leetcode/tree/master/maximum_product_subarray)
139+
#### [153. find minimum in rotated sorted array](https://github.com/hitzzc/go-leetcode/tree/master/find_minimum_in_rotated_sorted_array)
139140

140141

141142

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package find_minimum_in_rotated_sorted_array
2+
3+
func findMin(nums []int) int {
4+
return helper(nums, 0, len(nums)-1)
5+
}
6+
7+
func helper(nums []int, i, j int) int {
8+
if i == j {
9+
return nums[i]
10+
}
11+
if nums[i] < nums[j] {
12+
return nums[i]
13+
}
14+
mid := i + (j-i)/2
15+
if i == mid {
16+
return helper(nums, i+1, j)
17+
}
18+
if nums[i] < nums[mid] {
19+
return helper(nums, mid+1, j)
20+
}
21+
return helper(nums, i, mid)
22+
}

0 commit comments

Comments
 (0)