leetcode 162. Find Peak Element --------- java

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[] nums) {
        int len = nums.length;

        if( len ==1 || nums[0] > nums[1] )
            return 0;
        if( nums[len-2] < nums[len-1])
            return len-1;
        for( int i = 1 ; i < len-1 ; i++ ){

            if( nums[i] > nums[i-1] && nums[i] > nums[i+1] )
                return i;

            if( nums[i] > nums[i+1] )
                i++;
        }
        return 0;
    }
}

2、还可以使用二分查找。

public class Solution {
    public int findPeakElement(int[] nums) {
        for(int min = 0, max = nums.length -1, mid = max / 2 ; min < max ; mid = (min + max) / 2){
            if(min == mid) return nums[min] < nums[max] ? max : min;
            min = nums[mid] < nums[mid+1] ? mid : min;
            max = nums[mid] > nums[mid+1] ? mid : max;
        }
        return 0;
    }
}
时间: 2024-10-05 20:43:15

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

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

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

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 20170706

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

根据题目可知,输入为:一个相邻元素不相等的数列,输出为:其中一个(上)峰值的序号.并且要求时间复杂度为logn 分析:由于题目要求时间复杂度为logn,因此不能进行全部遍历.又因为只需要找到其中的一个峰值,那么,每次对半分,便可以达到logn的复杂度. 根据对半分的思路继续想,不难发现只要确定了中间位置的数是处在上升阶段还是下降阶段,就可以确定在某一侧必有一个峰值. 往复多次,即可找出两个点,其中一个一定处于某一个峰值上. java代码: 1 public int findPeakElement

LeetCode:Find Peak Element - 寻找一个数组内的顶点

1.题目名称 Find Peak Element(寻找一个数组内的顶点) 2.题目地址 https://leetcode.com/problems/find-peak-element/ 3.题目内容 英文: 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

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