package com.test.leetcode.easy;

/**
 * Created by ben on 2017/8/17.
 */
public class _374_GuessNumberHigherOrLower {

    public int guessNumber(int n) {
        int start = 1;
        int end = n;
        int mid;
        while (start <= end) {
            mid = (start + end) / 2;
            int g = guess(mid);
            if (g == 0) {
                return mid;
            } else if (g == 1) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return -1;
    }

    int guess(int num) {
        if (num == 6) {
            return 0;
        } else if (num > 6) {
            return 1;
        } else {
            return -1;
        }
    }
}

@@ -164,19 +164,51 @@
 int 
-start = 1;%0A
+l = 1, r = n;%0A        while (l %3C= r) %7B%0A    
     
@@ -219,17 +219,35 @@
 int 
-en
+mi
 d = 
-n;%0A
+l + (r - l) / 2;%0A    
     
@@ -254,19 +254,30 @@
     int 
+g = guess(
 mid
+)
 ;%0A      
@@ -282,98 +282,319 @@
     
-while (start %3C= end) %7B%0A            mid = (start + end) / 2;%0A            int g = guess(mid)
+    if (g == 0) return mid;%0A            else if (g == 1) %7B%0A                l = mid + 1;%0A            %7D else %7B%0A                r = mid - 1;%0A            %7D%0A        %7D%0A        return 0;%0A    %7D%0A%0A    public int guessNumber1(int n) %7B%0A        int i = 1, j = n;%0A        while (i %3C j) %7B%0A            int mid = i + (j - i) / 2
 ;%0A  
@@ -604,24 +604,33 @@
        if (g
+uess(mid)
  == 0) %7B%0A   
@@ -670,32 +670,41 @@
     %7D else if (g
+uess(mid)
  == 1) %7B%0A       
@@ -716,19 +716,17 @@
     
-end
+i
  = mid 
--
++
  1;%0A
@@ -766,23 +766,15 @@
     
-start
+j
  = mid
- + 1
 ;%0A  
@@ -806,26 +806,25 @@
      return 
--1
+i
 ;%0A    %7D%0A%0A   
@@ -841,24 +841,49 @@
 (int num) %7B%0A
+        int compare = 1;%0A
         if (
@@ -889,17 +889,23 @@
 (num == 
-6
+compare
 ) %7B%0A    
@@ -951,9 +951,15 @@
 m %3E 
-6
+compare
 ) %7B%0A
@@ -977,16 +977,17 @@
  return 
+-
 1;%0A     
@@ -1009,33 +1009,32 @@
          return 
--
 1;%0A        %7D%0A   

change 1 question