Skip to content

Commit 2ed6a34

Browse files
committed
Leetcode 374
1 parent c49ce84 commit 2ed6a34

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

301-400/374.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
## :pencil2:基础刷题之( 374. Guess Number Higher or Lower)
3+
<br>.
4+
**2020-05-13 星期二 开始吧 库里的深夜食堂**
5+
6+
### :pencil2:描述
7+
**一个猜大小的游戏,猜一个1-n之间的数,从接口获取接口,如果是0说明你猜对了返回结果,如果返回-1,太大了,如果返回1,太小了**
8+
****
9+
### :pencil2:题目实例
10+
11+
<a href="https://github.com/wuqinqiang/">
12+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/374.png">
13+
</a>
14+
15+
****
16+
### :pencil2:题目分析
17+
18+
**典型的二分查找,对于二分查找,可以通过目录:算法获取有关知识**
19+
20+
### :pencil2:最终实现代码
21+
22+
```php
23+
/**
24+
* The API guess is defined in the parent class.
25+
* @param num your guess
26+
* @return -1 if num is lower than the guess number
27+
* 1 if num is higher than the guess number
28+
* otherwise return 0
29+
* public function guess($num){}
30+
*/
31+
32+
class Solution extends GuessGame {
33+
/**
34+
* @param Integer $n
35+
* @return Integer
36+
*/
37+
function guessNumber($n) {
38+
$left=1;
39+
$right=$n;
40+
while($left<=$right){
41+
$middle=$left+(($right-$left)>>1);
42+
$res=$this->guess($middle);
43+
if($res===0){
44+
return $middle;
45+
}else if ($res===-1){
46+
$right=$middle-1;
47+
}else{
48+
$left=$middle+1;
49+
}
50+
}
51+
}
52+
}
53+
54+
```
55+
56+
#### 联系
57+
58+
<a href="https://github.com/wuqinqiang/">
59+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
60+
</a>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
- [Leetcode349](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/349.md)
220220
- [Leetcode350](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/350.md)
221221
- [Leetcode367](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/367.md)
222+
- [Leetcode374](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/374.md)
222223
- [Leetcode378](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/378.md)
223224
- [Leetcode371](https://github.com/wuqinqiang/Lettcode-php/blob/master/301-400/371.md)
224225
****

images/374.png

137 KB
Loading

0 commit comments

Comments
 (0)