【一天一道LeetCode】#374. Guess Number Higher or Lower
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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’ll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!Example:
n = 10, I pick 6.
Return 6.
来源: https://leetcode.com/problems/guess-number-higher-or-lower/
(二)解题
题目大意:猜数游戏,1~n里面的数,首先你心里想一个数num,让对方猜,对方每猜一个数你都要判断比num大还是小,直到对方猜对!
解题思路:采用二分搜索法来猜数效率最高。注意int guess(int num)函数用来返回大还是小。
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
class Solution {
public:
int guessNumber(int n) {
int i = 0;
int j = n;
//二分查找法
while(i<=j)
{
int mid = i+(j-i)/2;//取中点
int flag = guess(mid);//返回大于、小于或等于
if(flag == 0) return mid;
else if(flag == -1) j = mid-1;
else i = mid+1;
}
return 0;
}
};
【一天一道LeetCode】#374. Guess Number Higher or Lower的更多相关文章
- leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- [LeetCode] 374. 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 gues ...
- LeetCode 374. 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 gues ...
- Python [Leetcode 374]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 t ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小之二
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 四个常用的 Rewrite 使用范例
一.防盗链功能只这四行就实现了防盗链,原理是利用REFERER判断网页来源,缺点是REFERER容易伪造. RewriteEngine On RewriteCond %{HTTP_REFERER} ! ...
- tensorflow rnn 最简单实现代码
tensorflow rnn 最简单实现代码 #!/usr/bin/env python # -*- coding: utf-8 -*- import tensorflow as tf from te ...
- C++标准库之stack
C++库以提供"模板"为主.所谓模板,是指不必预先制定类型的函数或类.我们可以借助STL(标准模板库 Standard Template Library, STL)提供的高效算法来 ...
- Axios 使用文档
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 使用实例:http://www.cnblogs.com/coolslider/p/7838309.ht ...
- javascript requestAnimationFarme
今天看到一篇很好的文章推荐一下:原文地址:http://www.zhangxinxu.com/wordpress/?p=3695 CSS3动画那么强,requestAnimationFrame还有毛线 ...
- 02Vue2.0+生命周期
Vue生命周期是Vue对象从无到有再到无的一个过程,我们又是不仅要明白一个对象的使用, 同时也要知道一个对象怎么创建了,就比如Spring的生命周期,往往不只是面试官的考点, 同时在项目中也也可能常常 ...
- 初始化mysql数据库——Activiti BPM
package com.initialize; import org.activiti.engine.ProcessEngine; import org.activiti.engine.Process ...
- POJ 3050 Hopscotch DFS
The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of num ...
- ACM FatMouse' Trade
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containi ...
- 百钱买百鸡问题Java
//百钱买百鸡public class baiqianbaiji { static void BQBJ(int m,int n)//m为钱的总数,n为鸡数 { int z; for(int x = 0 ...