leetcode Find Peak Element python

Find Peak Element

A peak element is an element that is greater than its neighbors.

Given an input array where num[i] ≠ num[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 num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

python code:

class Solution:
# @param nums, an integer[]
# @return an integer
def findPeakElement(self, nums):
  if len(nums)==1 or nums[0]>nums[1]:             #边界条件判定
     return 0
  else:
    for i in range(1,len(nums)-1):
      if nums[i]>nums[i-1] and nums[i]>nums[i+1]:   #第2个到第n-1个的判定
        return i
    else:                                                                   #for正常结束,第n个为peak number
      return len(nums)-1

时间: 2024-09-28 08:44:57

leetcode Find Peak Element python的相关文章

[LeetCode]Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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 fi

[LeetCode] Find Peak Element 求数组的峰值

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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 fi

LeetCode: Find Peak Element 解题报告

Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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

Leetcode#162Find Peak Element

Find Peak Element Total Accepted: 26063 Total Submissions: 83491My Submissions Question Solution A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.

[LeetCode] Find Peak Element 二分搜索

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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 fi

LeetCode Find Peak Element [TBD]

说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &num) { int len = num.size(); if (len < 1) { return -1; } if (len == 1) { return 0; } bool asc = true; int idx = 0; int last = num[idx++]; while (idx

leetcode 【 Find Peak Element 】python 实现

题目: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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 i

LeetCode OJ 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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 fi

[LeetCode][JavaScript]Find Peak Element

Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[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