Skip to content

Commit 2ca8100

Browse files
authored
Merge pull request #17 from SamTienz/meet-up-24
Find Peak Element Recursive
2 parents ac37548 + d3e60bd commit 2ca8100

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

紀錄/24 20210406.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ func findPeakElement(_ nums: [Int]) -> Int {
4747
return low
4848
}
4949
```
50+
```Swift
51+
// 想說用recursive做看看 但發現好像只能抓到 Peak Element 不能找到當初的Index
52+
func findPeakElement(_ nums: [Int]) -> Int {
53+
func helper(_ low: Int, _ high: Int) -> Int {
54+
if low >= high {return low}
55+
let pivot = (high - low) / 2 + low
56+
if nums[pivot] > nums[pivot + 1] {
57+
return helper(low, pivot)
58+
} else {
59+
return helper(pivot + 1, high)
60+
}
61+
}
62+
return helper(0, nums.count - 1)
63+
}
64+
```
5065
* MEDIUM 153 Find Minimum in Rotated Sorted Array https://leetcode.com/problems/find-minimum-in-rotated-sorted-array
5166
* Template III
5267
* Article Binary Search Template III

0 commit comments

Comments
 (0)