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

You may imagine that num[-1] = num[n] = -∞.

For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.

思路:

  二分查找

我的思路:

public class Solution {
    public int findPeakElement(int[] num) {
        if(num == null || num.length == 0) return -1;
        int len = num.length;
        if(len == 1 || num[0] > num[1]) return 0;
        if(num[len - 1] > num[len - 2]) return len - 1;
        int left = 1;
        int right = len - 2;
        return getPeakIndex(num, left, right);

    }
    public int getPeakIndex(int[] num, int left, int right)
    {
        if(left > right) return -1;
        if(left == right)
        {
            if(num[left] > num[left - 1] && num[left] > num[left + 1]) return left;
            else return -1;
        }
        int mid = (left + right)/2;
        if(num[mid] > num[mid - 1] && num[mid] > num[mid + 1])  return mid;
        int index = getPeakIndex(num, left, mid - 1);
        if(index == -1)
            index = getPeakIndex(num, mid + 1, right);
        return index;
    }

}

改进后算法:

public class Solution {
    public int findPeakElement(int[] num) {
        if(num == null || num.length == 0) return -1;
        int len = num.length;
        if(len == 1) return 0;
        int left = 0;
        int right = len - 1;
        while(left <= right)
        {
            int mid = (left + right)/2;
            if(mid == 0)
            {
                if(num[mid] > num[mid+1])   return mid;
            }
            else if(mid == len - 1)
            {
                if(num[mid] > num[mid-1])   return mid;
            }
            else
            {
                if(num[mid] > num[mid-1] && num[mid] > num[mid+1]) return mid;
            }
            if(num[mid] < num[mid+1]) left = mid + 1;
            else right = mid;
        }
        return -1;

    }
}

他人代码:

class Solution {
public:
    int findPeakElement(const vector<int> &num) {
        int left=0,right=num.size()-1;
        while(left<=right){
            if(left==right)
                return left;
            int mid=(left+right)/2;
            if(num[mid]<num[mid+1])
                left=mid+1;
            else
                right=mid;
        }
    }
};

学习之处:

  • 首先肯定存在peak点,因为最左侧num[-1]=num[n]=负无穷大,肯定存在一个Peak
  • 若中间点不是peak,则分别朝着左右两侧上升的地方走
  • 左右left和right,都往着上升的地方走,相遇的地方就是peak点(这个有点难理解啊),没证明出来,但是一想是这么一回事
  • 由于peak点事肯定存在的,所以right = mid 不会引起死循环,总会跳出循环的。
时间: 2024-10-13 14:53:14

Find Peak Element的相关文章

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

162. Find Peak Element (Array; Divide-and-Conquer)

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

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 anyone of the peaks is fin

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

Find Peak Element(ARRAY - Devide-and-Conquer)

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

[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 (3 solutions)

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. You may imagine that num[-1] = num[n] = -∞. For example, in array [1, 2, 3, 1],