leetcode 374

这个题目很简单,但是要注意细节和对题目的理解,一开始我把guess函数的作用理解错了,第一版代码长这样:

 1 int guessNumber(int n)
 2 {
 3     int low = 1;
 4     int high = n;
 5
 6     while(low < high)
 7     {
 8         int mid = (high + low) / 2;
 9         int g = guess(mid);
10         if(g == 0)
11             return mid;
12         else if(g > 0)
13             low = mid + 1;
14         else
15             high = mid - 1;
16     }
17
18     return low;
19
20 }

在重新读了几次题目之后,第二版代码长这样:

 1 int guessNumber(int n)
 2 {
 3     int low = 1;
 4     int high = n;
 5
 6     while(low < high)
 7     {
 8         int mid = (high + low) / 2;
 9         int g = guess(mid);
10         if(g == 0)
11             return mid;
12         else if(g < 0)
13             low = mid + 1;
14         else
15             high = mid - 1;
16     }
17
18     return low;
19
20 }

但是还是不能正确通过,这很尴尬:查了半天发现是上面标注的那段代码有问题,我一直以为把代码写成最简单的数学表达式是一个好选择,但是这里显然不是这样:因为这样当high和low都比较大时,这是会导致溢出!所以有了第三个版本的代码:

int guessNumber(int n)
{
    int low = 1;
    int high = n;

    while(low < high)
    {
        int mid = low + (high - low) / 2;
        int g = guess(mid);
        if(g == 0)
            return mid;
        else if(g < 0)
            low = mid + 1;
        else
            high = mid - 1;
    }
    return low;
}
时间: 2024-10-12 16:47:27

leetcode 374的相关文章

[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

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 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 n

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

&lt;LeetCode OJ&gt; 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

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

我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0): -1 : 我的数字比较小 1 : 我的数字比较大 0 : 恭喜!你猜对了! 示例 : 输入: n = 10, pick = 6 输出: 6 相似题目:Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version

Leetcode 前 400 重点 250 题

这个重点题目是把Leetcode前400题进行精简,划分精简规则如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & 389,保留一个) 所有题目尽量保证客观公正,只是按大概率删除不常考题目,很多题目面经出现过, 但出现次数属于个位数或者只有一两家出现进行删除.所以如在面试中出现删除题目概不负责,这只是从概率上删除低频,简单题目. 旨在减轻大家的刷

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro