HTMLify
LeetCode - Guess Number Higher or Lower - Go
Views: 318 | Author: abh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Forward declaration of guess API. * @param num your guess * @return -1 if num is higher than the picked number * 1 if num is lower than the picked number * otherwise return 0 * func guess(num int) int; */ func guessNumber(n int) int { l, u, gn := 1, n, n for ;; { g := guess(gn) if g == 0 { return gn } if g == +1 { l = gn } if g == -1 { u = gn } gn = (l+u)/2 } } |