题目描述:

从1~n中,随便的拿出一个数字,你来猜测。

提示

提供一个guess(int num)的api,针对猜测的数字,返回三个数值。0,-1,1

  • 0;猜中返回num
  • -1:比猜测的数值小
  • 1:比猜测的数值大

例如:

n = 10, I pick 6.

Return 6.

原文描述:

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.

思路分析:

  • 明显是二分法的应用,关于二分法,看我之前的一片博客,讲的比较详细,链接如下:

    Java语言实现二分法

代码:

/* The guess API is defined in the parent class GuessGame.
   @param num, your guess
   @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
      int guess(int num); */

public class Solution extends GuessGame {
    public int guessNumber(int n) {
         int L = 1,R = n;
        while(L <= R){
            int mid = L + ((R - L) >> 1);
            int res = guess(mid);
            if(res == 0) return mid;
            else if(res == 1)  L = mid + 1;
            else R = mid - 1;
        }
        return L;
    }
}

更多leetcode题目,请看我的leetcode专栏。链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

【leetcode73】经典算法-Guess Number Higher or Lower的更多相关文章

  1. [LeetCode] 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 ...

  2. 374&375. Guess Number Higher or Lower 1&2

    做leetcode的题 We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You ...

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

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

  5. [LeetCode] 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 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  7. Guess Number Higher or Lower II--困惑

    今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/gue ...

  8. 每天一道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 ...

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

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

随机推荐

  1. linux tar解压命令

    linux下使用tar命令 解压语法:tar [主选项+辅选项] 文件或者目录 使用该命令时,主选项是必须要有的,它告诉tar要做什么事情,辅选项是辅助使用的,可以选用.主选项:c 创建新的档案文件. ...

  2. day06_JDBC学习笔记

    ============================================================ 一.JDBC概述 为什么要使用JDBC? JDBC:Java DataBase ...

  3. DOS/Windows 文本格式与 Unix 文本各式转换

    命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2 ...

  4. Node.js HTTP

    稳定性: 3 - 稳定 使用 HTTP 服务器或客户端功能必须调用 require('http'). Node 里的 HTTP 接口支持协议里原本比较难用的特性.特别是很大的或块编码的消息.这些接口不 ...

  5. Android Studio: You need to use a Theme.AppCompat theme (or descendant) with this activity.

    错误描述为: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with ...

  6. Android的四大组件及应用安装安全问题(4)

    Android的四大组件及组件间通信 如果想对四大组件有深入的了解,那永远不要停留在一些条条干干的SDK API的认识,需要了解他的通讯,他的复用,他的边界问题,这样才会对四大组件有透明的认识. 四大 ...

  7. Errors occurred during the build. Errors running builder 'JavaScript Validator' on project '项目名'.

    把JavaScript Validator去掉.去掉的方法是:选择一个项目--右键Properties--Builders(排第二)--点一下右侧会有四项--取消第一项"JavaScript ...

  8. Dubbo框架应用之(二)--服务治理

    Dubbo服务治理全貌图 当我们现有ITOO平台系统的业务随着用户的逐渐增大,设计的业务越来越广,系统会异常的复杂,在大规模服务之前,我们可以采用的是RMI或Hessian等工具,暴露和引用远程服务, ...

  9. 支付平台PCI认证整改

    五大国际卡组织推出的PCI支付认证.支付平台改造方案.

  10. 六星经典CSAPP-笔记(3)程序的机器级表示

    1.前言 IA32机器码以及汇编代码都与原始的C代码有很大不同,因为一些状态对于C程序员来说是隐藏的.例如包含下一条要执行代码的内存位置的程序指针(program counter or PC)以及8个 ...