LeetCode 852 Peak Index in a Mountain Array 解题报告

题目要求

Let‘s call an array A a mountain if the following properties hold:

  • A.length >= 3
  • There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]

Given an array that is definitely a mountain, return any such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

题目分析及思路

题目给出一个整数数组,满足数组长度大于等于3且存在一个元素,它前面的元素依次递增,后面的元素依次递减。要求返回该元素的的索引。遍历数组,当某元素大于后一个元素则该元素即为所求元素。

python代码?

class Solution:

def peakIndexInMountainArray(self, A):

"""

:type A: List[int]

:rtype: int

"""

l = len(A)

for i in range(1,l):

if A[i-1]>A[i]:

return i-1

原文地址:https://www.cnblogs.com/yao1996/p/10322633.html

时间: 2024-10-10 02:04:44

LeetCode 852 Peak Index in a Mountain Array 解题报告的相关文章

LeetCode 852. Peak Index in a Mountain Array(C++)

Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] Given an array that is definitely

【Leetcode_easy】852. Peak Index in a Mountain Array

problem 852. Peak Index in a Mountain Array 参考 1. Leetcode_easy_852. Peak Index in a Mountain Array; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11215023.html

852. Peak Index in a Mountain Array

1 class Solution 2 { 3 public: 4 int peakIndexInMountainArray(vector<int>& A) 5 { 6 return max_element(A.begin(),A.end())-A.begin(); 7 } 8 }; 这题很简单,问题不大. 原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9193637.html

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

LeetCode: Search in Rotated Sorted Array 解题报告

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】33. Search in Rotated Sorted Array 解题小结

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

【LeetCode】26. Remove Duplicates from Sorted Array 解题小结

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array n