Advertisement
sweet1cris

Untitled

Jan 9th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. class Solution {
  2.     /**
  3.      * @param A: An integers array.
  4.      * @return: return any of peek positions.
  5.      */
  6.     public int findPeak(int[] A) {
  7.         // write your code here
  8.         int start = 1, end = A.length-2; // 1.答案在之间,2.不会出界
  9.         while(start + 1 <  end) {
  10.             int mid = (start + end) / 2;
  11.             if(A[mid] < A[mid - 1]) {
  12.                 end = mid;
  13.             } else if(A[mid] < A[mid + 1]) {
  14.                 start = mid;
  15.             } else {
  16.                 end = mid;
  17.             }
  18.         }
  19.         if(A[start] < A[end]) {
  20.             return end;
  21.         } else {
  22.             return start;
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement