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



我们正在玩一个猜数字游戏。 游戏规则如下:
我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。
每次你猜错了,我会告诉你这个数字是大了还是小了。
你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-11 或 0):

-1 : 我的数字比较小
 1 : 我的数字比较大
 0 : 恭喜!你猜对了!

示例 :

输入: n = 10, pick = 6
输出: 6

相似题目:Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version)这题的题解看链接上面的,思路是一样的。

AC代码:
/* 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;
        int R = n;
        while(L<R){
            int mid = (L+R)>>>1;
            int guessAns = guess(mid);
            if(guessAns==1){
                L = mid+1;
            }else{
                R = mid;
            }
        }
        return L;
    }
}


原文地址:https://www.cnblogs.com/qinyuguan/p/11415921.html

时间: 2024-08-28 21:52:41

Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)的相关文章

leetcode 374. 猜数字大小(python)

我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0): -1 : 我的数字比较小 1 : 我的数字比较大 0 : 恭喜!你猜对了!示例 : 输入: n = 10, pick = 6输出: 6 class Solution(object): def guessNumber(self, n): "&q

猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了, 如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)

产生0-100之间的随机数,包括0和100 double d = Math.random() * 100; int a = (int)Math.round(d); ------------------------------------- package 水仙花数; import java.util.Scanner;/* 猜数字大小游戏,用户输入一个数字,如果大了就显示大了,如果小了就显示小了,如果对了就提示正确(补充难度,只有5次机会,限制数字的范围在百位以内)*/public class t

LeetCode Guess Number Higher or Lower II

原题链接在这里:https://leetcode.com/problems/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 guess which number I picked. Every time you guess wrong, I'll tell you whether

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My Submissions Total Accepted: 1546 Total Submissions: 5408 Difficulty: Medium We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess w

374. Guess Number Higher or Lower

/* * 374. Guess Number Higher or Lower * 2016-7-17 by Mingyang */ public int guessNumber(int n) { if (guess(n) == 0) return n; int left = 1, right = n; while (left < right) { int mid = left + (right - left) / 2, t = guess(mid); if (t == 0) return mid

LeetCode:Bulls and Cows - 猜数字游戏

1.题目名称 Bulls and Cows(猜数字游戏) 2.题目地址 https://leetcode.com/problems/bulls-and-cows/ 3.题目内容 英文:You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret number and ask your friend to guess it, each time your friend g

[Swift]LeetCode374. 猜数字大小 | 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 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) wh

Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有矩阵 matrix 如下: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ] 给定 target = 5,返回 true. 给定 target = 20,返回 

[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 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) wh