> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_search/problem-374-guess-number-higher-or-lower.md).

# Problem 374: Guess Number Higher or Lower

> <https://leetcode.com/problems/guess-number-higher-or-lower/#/description>

## 思路

* 常规题目，只是用一个 API 做了一些掩饰而已

```java
/* 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 start = 1, end = n, mid;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            int guessRst = guess(mid);
            if (guessRst == 0) {
                return mid;
            } else if (guessRst == - 1) {
                end = mid;
            } else {
                start = mid;
            }
        }

        if (guess(start) == 0) {
            return start;
        } else {
            return end;
        }    
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_search/problem-374-guess-number-higher-or-lower.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
