|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[374. 猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-tocm/)** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「二分」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +猜数字游戏的规则如下: |
| 10 | +* 每轮游戏,我都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。 |
| 11 | +* 如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。 |
| 12 | + |
| 13 | +你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0): |
| 14 | + |
| 15 | +* -1:我选出的数字比你猜的数字小 pick < num |
| 16 | +* 1:我选出的数字比你猜的数字大 pick > num |
| 17 | +* 0:我选出的数字和你猜的数字一样。恭喜!你猜对了!pick == num |
| 18 | + |
| 19 | +返回我选出的数字。 |
| 20 | + |
| 21 | +示例 1: |
| 22 | +``` |
| 23 | +输入:n = 10, pick = 6 |
| 24 | +输出:6 |
| 25 | +``` |
| 26 | +示例 2: |
| 27 | +``` |
| 28 | +输入:n = 1, pick = 1 |
| 29 | +输出:1 |
| 30 | +``` |
| 31 | +示例 3: |
| 32 | +``` |
| 33 | +输入:n = 2, pick = 1 |
| 34 | +输出:1 |
| 35 | +``` |
| 36 | +示例 4: |
| 37 | +``` |
| 38 | +输入:n = 2, pick = 2 |
| 39 | +输出:2 |
| 40 | +``` |
| 41 | + |
| 42 | +提示: |
| 43 | +* 1 <= n <= $2^{31}$ - 1 |
| 44 | +* 1 <= pick <= n |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### 二分 |
| 49 | + |
| 50 | +端午安康呀 🍭🍭🍭 |
| 51 | + |
| 52 | +今天的题目表达不太好 🤣,还是逐字阅读才明白是啥意思 🤣 |
| 53 | + |
| 54 | +就当做是因为题目本身要比 [278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/) 简单(不需要考虑完全相同,没有二段性的情况),所以在阅读上增加了难度吧 🤣 |
| 55 | + |
| 56 | +一道交互题,根据题意可知,是尽可能少调用 `guess` 方法来找到分割点。 |
| 57 | + |
| 58 | +直接使用 `guess` 当做 `check` 函数进行二分即可。 |
| 59 | + |
| 60 | +另外根据数据范围需要注意计算 `mid` 时的爆 `int` 问题,可以通过使用类似 `l + (r - l) / 2` 的做法解决,也可以通过一个临时 `long` 来解决。 |
| 61 | + |
| 62 | +代码: |
| 63 | +```Java [] |
| 64 | +public class Solution extends GuessGame { |
| 65 | + public int guessNumber(int n) { |
| 66 | + int l = 1, r = n; |
| 67 | + while (l < r) { |
| 68 | + long tmp = (long)l + r>> 1; |
| 69 | + int mid = (int)tmp; |
| 70 | + if (guess(mid) <= 0) { |
| 71 | + r = mid; |
| 72 | + } else { |
| 73 | + l = mid + 1; |
| 74 | + } |
| 75 | + } |
| 76 | + return r; |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | +```Java [] |
| 81 | +public class Solution extends GuessGame { |
| 82 | + public int guessNumber(int n) { |
| 83 | + int l = 1, r = n; |
| 84 | + while (l < r) { |
| 85 | + long tmp = (long)l + r + 1 >> 1; |
| 86 | + int mid = (int)tmp; |
| 87 | + if (guess(mid) >= 0) { |
| 88 | + l = mid; |
| 89 | + } else { |
| 90 | + r = mid - 1; |
| 91 | + } |
| 92 | + } |
| 93 | + return r; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | +* 时间复杂度:$O(\log{n})$ |
| 98 | +* 空间复杂度:$O(1)$ |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +### 其他「二分」相关题解 |
| 103 | + |
| 104 | +* 二分模板 |
| 105 | + [29. 两数相除](https://leetcode-cn.com/problems/divide-two-integers/) : [二分 + 倍增乘法解法(含模板)](https://leetcode-cn.com/problems/divide-two-integers/solution/shua-chuan-lc-er-fen-bei-zeng-cheng-fa-j-m73b/) |
| 106 | + |
| 107 | +* 二分模板题 |
| 108 | + [278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/) : [使用交互函数充当 check 进行二分](https://leetcode-cn.com/problems/first-bad-version/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-8hpv/) |
| 109 | + |
| 110 | + [374. 猜数字大小](https://leetcode-cn.com/problems/guess-number-higher-or-lower/) : [使用交互函数充当 check 进行二分](https://leetcode-cn.com/problems/guess-number-higher-or-lower/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-tocm/) |
| 111 | + |
| 112 | +* 二分本质 & 恢复二段性处理 |
| 113 | + |
| 114 | + [33. 搜索旋转排序数组(找目标值)](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/) : [严格 O(logN),一起看清二分的本质](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/shua-chuan-lc-yan-ge-ologn100yi-qi-kan-q-xifo/) |
| 115 | + |
| 116 | + [81. 搜索旋转排序数组 II(找目标值)](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/) : [详解为何元素相同会导致 O(n),一起看清二分的本质](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-xtam4/) |
| 117 | + |
| 118 | + [153. 寻找旋转排序数组中的最小值(找最小值)](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) : [严格 O(logN),一起看清二分的本质](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/gong-shui-san-xie-yan-ge-olognyi-qi-kan-6d969/) |
| 119 | + |
| 120 | + [154. 寻找旋转排序数组中的最小值 II(找最小值)](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/) : [详解为何元素相同会导致 O(n),一起看清二分的本质](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-7xbty/) |
| 121 | + |
| 122 | +* 二分 check 函数如何确定 |
| 123 | + [34. 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/) : [考察对「二分」的理解,以及 check 函数的「大于 小于」怎么写](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/solution/gong-shui-san-xie-kao-cha-dui-er-fen-de-86bk0/) |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +### 最后 |
| 128 | + |
| 129 | +这是我们「刷穿 LeetCode」系列文章的第 `No.374` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。 |
| 130 | + |
| 131 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 132 | + |
| 133 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 134 | + |
| 135 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 136 | + |
0 commit comments