374. Guess Number Higher or Lower

binary search. 开始偷懒了一下mid没有写成这样:mid = l + (r - l) / 2,于是卡了很久,简直报应= =

 1     public int guessNumber(int n) {
 2         int l = 1;
 3         int r = n;
 4         int mid = l + (r - l) / 2;
 5         int guessAns = guess(mid);
 6         while(guessAns != 0) {
 7             if(guessAns < 0) {
 8                 r = mid - 1;
 9             } else {
10                 l = mid + 1;
11             }
12             mid = l + (r - l) / 2;
13             guessAns = guess(mid);
14         }
15         return mid;
16     }

 

posted @ 2016-07-20 07:29  warmland  阅读(116)  评论(0编辑  收藏  举报