75. Find Peak Element

c++ code

class Solution {
public:
    /*
     * @param A: An integers array.
     * @return: return any of peek position.
     */
    int findPeak(vector& nums) {
        // write your code here
        if (nums.empty()) {
           return -1;
        }
        int start = 0, end = nums.size() - 1, mid = 0;
        while (start + 1 < end) {
           mid = (end - start) / 2 + start;
           if (nums[mid] < nums[mid + 1]) {
              start = mid;
           } else {
              end = mid;
           }
        }
        return nums[start] > nums[end] ? start : end;
    }
};

你可能感兴趣的:(leetcode)