美文网首页
374. Guess Number Higher or Lowe

374. Guess Number Higher or Lowe

作者: namelessEcho | 来源:发表于2017-09-29 21:56 被阅读0次

    换壳二分

    /* The guess API is defined in the parent class GuessGame.
       @param num, your guess
       @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
          int guess(int num); */
    
    public class Solution extends GuessGame {
        public int guessNumber(int n) {
            int hi =n;
            int lo =1;
            int result = 0;
            while(lo<=hi)
            {
                int mid = lo+(hi-lo)/2;
                if(guess(mid)==0)
                {
                    result=mid;
                    break;
                }
                else if(guess(mid)==-1)
                {
                    hi=mid-1;
                }
                else
                    lo=mid+1;
            }
            return result;
        }
    }
    

    相关文章

      网友评论

          本文标题:374. Guess Number Higher or Lowe

          本文链接:https://www.haomeiwen.com/subject/iuywextx.html