首页 > 代码库 > LeetCode Find Peak Element [TBD]

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 < len) {            int cur = num[idx];            if (asc) {                if (cur < last) {                    return idx - 1;                }            } else {                if (cur > last) {                    asc = true;                   }            }            last = cur;            idx++;        }        if (asc) {            return idx - 1;        }        return -1;    }};

 

LeetCode Find Peak Element [TBD]