作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/guess-number-higher-or-lower/#/description

题目描述

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. -1 : My number is lower
  2. 1 : My number is higher
  3. 0 : Congrats! You got it!

Example:

  1. n = 10, I pick 6.
  2. Return 6.

题目大意

从1~n中取了一个数字,现在给出了guess()函数,要猜这个数字是多少。

解题方法

做这个题的重点是明白guess()函数,题目说了是取了一个数字,去猜这个数字,guess()是的数字大了还是小了。。明白这个意思了么。

所以这个题是标准的二分查找。

  1. /* The guess API is defined in the parent class GuessGame.
  2. @param num, your guess
  3. @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
  4. int guess(int num); */
  5. public class Solution extends GuessGame {
  6. public int guessNumber(int n) {
  7. int low = 1;
  8. int high = n;
  9. int mid = 0;
  10. while(low <= high){
  11. mid = low + (high - low) / 2;
  12. if(guess(mid) == -1){
  13. high = mid - 1;
  14. }else if(guess(mid) == 1){
  15. low = mid + 1;
  16. }else{
  17. return mid;
  18. }
  19. }
  20. return mid;
  21. }
  22. }

python解法如下:

  1. # The guess API is already defined for you.
  2. # @param num, your guess
  3. # @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
  4. # def guess(num):
  5. class Solution(object):
  6. def guessNumber(self, n):
  7. """
  8. :type n: int
  9. :rtype: int
  10. """
  11. left, right = 1, n #[left, right]
  12. mid = left
  13. while left <= right:
  14. mid = (right + left) / 2
  15. res = guess(mid)
  16. if res == 0:
  17. return mid
  18. elif res == 1:
  19. left = mid + 1
  20. else:
  21. right = mid - 1
  22. return mid

日期

2017 年 5 月 10 日
2018 年 11 月 23 日 —— 这就星期五了??

【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)的更多相关文章

  1. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】933. Number of Recent Calls 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...

  7. 【LeetCode】349. Intersection of Two Arrays 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:Java解法,HashSet 方法二:Pyt ...

  8. [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 ...

  9. Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

    Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...

随机推荐

  1. eggNOG 5.0数据库介绍

    目录 1. eggNOG简介 2. eggNOG-Mapper注释原理 3. eggNOG 5.0数据资源 4. eggNOG-Mapper使用 5. NOG.KOG.COG.KEGG.GO区别? 1 ...

  2. LetNet、Alex、VggNet分析及其pytorch实现

    简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...

  3. SpringBoot整合Shiro 一:搭建环境

    Java项目的安全框架一般使用 shiro 与 spring security 具体怎么选择可以参考文章:安全框架 Shiro 和 Spring Security 如何选择 我这里选择使用Shiro ...

  4. A Child's History of England.18

    But, although she was a gentle lady, in all things worthy to be beloved - good, beautiful, sensible, ...

  5. 银联acp手机支付总结

    总结: 1.手机调用后台服务端接口,获取银联返回的流水号tn 银联支付是请求后台,后台向银联下单,返回交易流水号,然后返回给用户,用户通过这个交易流水号,向银联发送请求,获取订单信息,然后再填写银行卡 ...

  6. OS开发之Objective-C与JavaScript的交互

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  7. FindUserByPageServlet

    package com.hopetesting.web.servlet;import com.hopetesting.domain.PageBean;import com.hopetesting.do ...

  8. Java常用类,这一次帮你总结好!

    常用类 常用类概述: 内部类 Object类 包装类 数学类 时间类 字符串 String Builder和StringBuffer DecimalFormat 一.内部类 概念:在一个类内部再定义一 ...

  9. Linux目录终章,单用户模式修改密码、环境变量、第三方软件安装

    目录 今日内容概要 内容详细 解析映射文件 磁盘挂载文件 开机加载脚本 系统启动级别 使用单用户模式修改密码 变量加载文件 登录提示信息 第三方软件安装目录(编译安装目录) 系统日志目录 保存系统运行 ...

  10. InnoDB学习(四)之RedoLog和UndoLog

    BinLog是MySQL Server层的日志,所有的MySQL存储引擎都支持BinLog.BinLog可以支持主从复制和数据恢复,但是对事务的ACID特性支持比较差.InnoDB存储引擎引入Redo ...