leetcode-Find Peak Element-162

输入一个序列,求第一个a[i]>a[i-1]&&a[i]>a[i+1]的元素

遍历一遍

 1 class Solution {
 2 public:
 3     int findPeakElement(vector<int>& nums) {
 4         if(nums.size()==0) return -1;
 5         if(nums.size()==1) return 0;
 6         for(int i=0;i<nums.size();i++){
 7             if(i==0){
 8                 if(nums[i]>nums[i+1]) return i;
 9                 continue;
10             }
11             if(i==nums.size()-1){
12                 if(nums[i]>nums[i-1]) return i;
13                 continue;
14             }
15             if(nums[i]>nums[i-1]&&nums[i]>nums[i+1]) return i;
16         }
17         return  -1;
18     }
19 };
时间: 2024-10-03 13:20:49

leetcode-Find Peak Element-162的相关文章

[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 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

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 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 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 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