mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 16:36:41 +08:00
Add solution 0374
This commit is contained in:
@ -0,0 +1,20 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
/**
|
||||
* Forward declaration of guess API.
|
||||
* @param num your guess
|
||||
* @return -1 if num is lower than the guess number
|
||||
* 1 if num is higher than the guess number
|
||||
* otherwise return 0
|
||||
* func guess(num int) int;
|
||||
*/
|
||||
|
||||
func guessNumber(n int) int {
|
||||
return sort.Search(n, func(x int) bool { return guess(x) <= 0 })
|
||||
}
|
||||
|
||||
func guess(num int) int {
|
||||
return 0
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question374 struct {
|
||||
para374
|
||||
ans374
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para374 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans374 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem374(t *testing.T) {
|
||||
|
||||
qs := []question374{
|
||||
|
||||
{
|
||||
para374{10},
|
||||
ans374{6},
|
||||
},
|
||||
|
||||
{
|
||||
para374{1},
|
||||
ans374{1},
|
||||
},
|
||||
|
||||
{
|
||||
para374{2},
|
||||
ans374{1},
|
||||
},
|
||||
|
||||
{
|
||||
para374{2},
|
||||
ans374{2},
|
||||
},
|
||||
// 如需多个测试,可以复制上方元素。
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 374------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans374, q.para374
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, guessNumber(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
94
leetcode/0374.Guess-Number-Higher-or-Lower/README.md
Normal file
94
leetcode/0374.Guess-Number-Higher-or-Lower/README.md
Normal file
@ -0,0 +1,94 @@
|
||||
# [374. Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/)
|
||||
|
||||
## 题目
|
||||
|
||||
We are playing the Guess Game. The game is as follows:
|
||||
|
||||
I pick a number from `1` to `n`. You have to guess which number I picked.
|
||||
|
||||
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
|
||||
|
||||
You call a pre-defined API `int guess(int num)`, which returns 3 possible results:
|
||||
|
||||
- `1`: The number I picked is lower than your guess (i.e. `pick < num`).
|
||||
- `1`: The number I picked is higher than your guess (i.e. `pick > num`).
|
||||
- `0`: The number I picked is equal to your guess (i.e. `pick == num`).
|
||||
|
||||
Return *the number that I picked*.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = 10, pick = 6
|
||||
Output: 6
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = 1, pick = 1
|
||||
Output: 1
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: n = 2, pick = 1
|
||||
Output: 1
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: n = 2, pick = 2
|
||||
Output: 2
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n <= 231 - 1`
|
||||
- `1 <= pick <= n`
|
||||
|
||||
## 题目大意
|
||||
|
||||
猜数字游戏的规则如下:
|
||||
|
||||
- 每轮游戏,我都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。
|
||||
- 如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。
|
||||
|
||||
你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):
|
||||
|
||||
- 1:我选出的数字比你猜的数字小 pick < num
|
||||
- 1:我选出的数字比你猜的数字大 pick > num
|
||||
- 0:我选出的数字和你猜的数字一样。恭喜!你猜对了!pick == num
|
||||
|
||||
返回我选出的数字。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题是简单题,和小时候玩的猜大猜小的游戏一样。思路很简单,二分查找即可。这一题和第 278 题类似。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
/**
|
||||
* Forward declaration of guess API.
|
||||
* @param num your guess
|
||||
* @return -1 if num is lower than the guess number
|
||||
* 1 if num is higher than the guess number
|
||||
* otherwise return 0
|
||||
* func guess(num int) int;
|
||||
*/
|
||||
|
||||
func guessNumber(n int) int {
|
||||
return sort.Search(n, func(x int) bool { return guess(x) <= 0 })
|
||||
}
|
||||
|
||||
func guess(num int) int {
|
||||
return 0
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user