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 a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].

Example 1:

Input: [0,1,0]
Output: 1

Example 2:

Input: [0,2,1,0]
Output: 1

Note:

  1. 3 <= A.length <= 10000
  2. 0 <= A[i] <= 10^6
  3. A is a mountain, as defined above.

二分法:

class Solution{
public:
        int peakIndexInMountainArray(vector<int>& a){
            int beg = 1,end = a.size();
            int mid = (beg + end) / 2;

            while(beg <= end){
                if(a[mid] < a[mid - 1]){
                    end = mid - 1;
                }
                else if(a[mid] < a[mid + 1]){
                    beg = mid + 1;
                }
                else{
                    break;
                }
                mid = (beg + end) / 2;
            }
            return mid;
        }
};

最大值法

class Solution{
public:
        int peakIndexInMountainArray(vector<int>& a){
            int max_elem = *max_element(a.begin(), a.end());
            int pos;
            for(pos = 0; pos < a.size(); ++pos){
                if(a[pos] == max_elem)
                    break;
            }

            return pos;
        }
};

原文地址:https://www.cnblogs.com/Mayfly-nymph/p/11273740.html

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

LeetCode 852. Peak Index in a Mountain Array(C++)的相关文章

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 defin

【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】Kth Largest Element in an Array (middle)☆

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

LeetCode:26. Remove Duplicates from Sorted Array(Easy)

1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度 注意:不能重新创建一个数组,空间复杂度为O(1) 3. 解题思路 使用指针j来遍历数组,i用来计数.初始时,i指向nums[0],j指向nums[1]. 当nums[i] != nums[j]时,i++,且nums[i]=nums[j],

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 du

leetcode 题解: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 A

leetcode——Longest Common Prefix 最长公共前缀(AC)

Write a function to find the longest common prefix string amongst an array of strings. 其实做起来会感觉很简单,需要注意的是要考虑效率的问题,毕竟可能是很长的字符串数组,所以可以考虑选取所有字符串中最短的那个来首先进行比较,因为最长公共子串肯定不会大于其长度,这样避免了字符串之间长度差异很大造成的效率损失,然后每次比较之后最长公共子串的长度也永远不会大于最短的那个字符串,只会不变或减小,只要遍历字符串数组,挨个

golang之 Array(数组)

目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range遍历 五.多维数组 1. 二维数组的定义 2. 二维数组的遍历 六.数组是值类型 七.数组的其他相关方法 一.Array(数组) 数组是同一种数据类型元素的集合. 在Go语言中,数组从声明时就确定,使用时可以修改数组成员,但是数组大小不可变化 二.数组的定义 1. 基本语法 // 基本语法: var