From bf043b8b17193061b243b9af1265b1d13dee4e3d Mon Sep 17 00:00:00 2001 From: YDZ Date: Tue, 1 Oct 2019 14:32:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20162?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../162. Find Peak Element.go | 43 +++++++++++ .../162. Find Peak Element_test.go | 72 +++++++++++++++++++ Algorithms/0162. Find Peak Element/README.md | 43 +++++++++++ 3 files changed, 158 insertions(+) create mode 100644 Algorithms/0162. Find Peak Element/162. Find Peak Element.go create mode 100644 Algorithms/0162. Find Peak Element/162. Find Peak Element_test.go create mode 100755 Algorithms/0162. Find Peak Element/README.md diff --git a/Algorithms/0162. Find Peak Element/162. Find Peak Element.go b/Algorithms/0162. Find Peak Element/162. Find Peak Element.go new file mode 100644 index 00000000..c36e4a7f --- /dev/null +++ b/Algorithms/0162. Find Peak Element/162. Find Peak Element.go @@ -0,0 +1,43 @@ +package leetcode + +// 解法一 二分 +func findPeakElement(nums []int) int { + if len(nums) == 0 || len(nums) == 1 { + return 0 + } + low, high := 0, len(nums)-1 + for low <= high { + mid := low + (high-low)>>1 + if (mid == len(nums)-1 && nums[mid-1] < nums[mid]) || (mid > 0 && nums[mid-1] < nums[mid] && (mid <= len(nums)-2 && nums[mid+1] < nums[mid])) || (mid == 0 && nums[1] < nums[0]) { + return mid + } + if mid > 0 && nums[mid-1] < nums[mid] { + low = mid + 1 + } + if mid > 0 && nums[mid-1] > nums[mid] { + high = mid - 1 + } + if mid == low { + low++ + } + if mid == high { + high-- + } + } + return -1 +} + +// 解法二 二分 +func findPeakElement1(nums []int) int { + low, high := 0, len(nums)-1 + for low < high { + mid := low + (high-low)>>1 + // 如果 mid 较大,则左侧存在峰值,high = m,如果 mid + 1 较大,则右侧存在峰值,low = mid + 1 + if nums[mid] > nums[mid+1] { + high = mid + } else { + low = mid + 1 + } + } + return low +} diff --git a/Algorithms/0162. Find Peak Element/162. Find Peak Element_test.go b/Algorithms/0162. Find Peak Element/162. Find Peak Element_test.go new file mode 100644 index 00000000..f5ef72bb --- /dev/null +++ b/Algorithms/0162. Find Peak Element/162. Find Peak Element_test.go @@ -0,0 +1,72 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question162 struct { + para162 + ans162 +} + +// para 是参数 +// one 代表第一个参数 +type para162 struct { + one []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans162 struct { + one int +} + +func Test_Problem162(t *testing.T) { + + qs := []question162{ + + question162{ + para162{[]int{2, 1, 2}}, + ans162{0}, + }, + + question162{ + para162{[]int{3, 2, 1}}, + ans162{0}, + }, + + question162{ + para162{[]int{1, 2}}, + ans162{1}, + }, + + question162{ + para162{[]int{2, 1}}, + ans162{0}, + }, + + question162{ + para162{[]int{1}}, + ans162{0}, + }, + + question162{ + para162{[]int{1, 2, 3, 1}}, + ans162{2}, + }, + + question162{ + para162{[]int{1, 2, 1, 3, 5, 6, 4}}, + ans162{5}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 162------------------------\n") + + for _, q := range qs { + _, p := q.ans162, q.para162 + fmt.Printf("【input】:%v 【output】:%v\n", p, findPeakElement(p.one)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0162. Find Peak Element/README.md b/Algorithms/0162. Find Peak Element/README.md new file mode 100755 index 00000000..9304f0ab --- /dev/null +++ b/Algorithms/0162. Find Peak Element/README.md @@ -0,0 +1,43 @@ +# [162. Find Peak Element](https://leetcode.com/problems/find-peak-element/) + + +## 题目: + +A peak element is an element that is greater than its neighbors. + +Given an input array `nums`, where `nums[i] ≠ nums[i+1]`, find a peak element and return its index. + +The array may contain multiple peaks, in that case return the index to any one of the peaks is fine. + +You may imagine that `nums[-1] = nums[n] = -∞`. + +**Example 1:** + + Input: nums = [1,2,3,1] + Output: 2 + Explanation: 3 is a peak element and your function should return the index number 2. + +**Example 2:** + + Input: nums = [1,2,1,3,5,6,4] + Output: 1 or 5 + Explanation: Your function can return either index number 1 where the peak element is 2, + or index number 5 where the peak element is 6. + +**Note:** + +Your solution should be in logarithmic complexity. + +## 题目大意 + +峰值元素是指其值大于左右相邻值的元素。给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。你可以假设 nums[-1] = nums[n] = -∞。 + +说明: + +- 你的解法应该是 O(logN) 时间复杂度的。 + + +## 解题思路 + +- 给出一个数组,数组里面存在多个“山峰”,(山峰的定义是,下标 `i` 比 `i-1`、`i+1` 位置上的元素都要大),找到这个“山峰”,并输出其中一个山峰的下标。 +- 这一题是第 852 题的伪加强版,第 852 题中只存在一个山峰,这一题存在多个山峰。但是实际上搜索的代码是一样的,因为此题只要求随便输出一个山峰的下标即可。思路同第 852 题。