374. Guess Number Higher or Lower

Easy

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 (-11, or 0):

-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!

Example :

Input: n = 10, pick = 6
Output: 6
package leetcode.easy;
/* 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); */ class GuessGame {
public int guess(int num) {
if (6 > num) {
return -1;
} else if (6 == num) {
return 0;
} else {
return 1;
}
}
} public class GuessNumberHigherOrLower extends GuessGame {
public int guessNumber1(int n) {
for (int i = 1; i < n; i++) {
if (guess(i) == 0) {
return i;
}
}
return n;
} public int guessNumber2(int n) {
int low = 1;
int high = n;
while (low <= high) {
int mid = low + (high - low) / 2;
int res = guess(mid);
if (res == 0) {
return mid;
} else if (res < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
} public int guessNumber3(int n) {
int low = 1;
int high = n;
while (low <= high) {
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
int res1 = guess(mid1);
int res2 = guess(mid2);
if (res1 == 0) {
return mid1;
}
if (res2 == 0) {
return mid2;
} else if (res1 < 0) {
high = mid1 - 1;
} else if (res2 > 0) {
low = mid2 + 1;
} else {
low = mid1 + 1;
high = mid2 - 1;
}
}
return -1;
} @org.junit.Test
public void test() {
GuessNumberHigherOrLower guessNumberHigherOrLower = new GuessNumberHigherOrLower();
System.out.println(guessNumberHigherOrLower.guessNumber1(10));
System.out.println(guessNumberHigherOrLower.guessNumber2(10));
System.out.println(guessNumberHigherOrLower.guessNumber3(10));
}
}

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

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

  4. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

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

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

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

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

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

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

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

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

随机推荐

  1. python - django (视图)

    # """ 一:# 视图 (接收请求返回响应的那部分) FBV版:基于 函数的 请求 CBV版:基于 类的 请求 注册方法: url(r'^add_publisher/' ...

  2. js正则验证input输入框有空格时提示直接去除空格

    <input type="text" id="test"/> <input type="button" value=&qu ...

  3. LightOJ - 1311 - Unlucky Bird(相遇问题)

    链接: https://vjudge.net/problem/LightOJ-1311 题意: A bird was flying on a train line, singing and passi ...

  4. .net web api 返回的是xml

    var result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(dto2) ...

  5. [matlab工具箱] 神经网络Neural Net

    //目的是学习在BP神经网络的基础上添加遗传算法,蚁群算法等优化算法来优化网络,这是后话. 先简单了解了MATLAB中的神经网络工具箱,工具箱功能还是非常强大的,已经可以拟合出非常多的曲线来分析了. ...

  6. [HAOI2015][bzoj 4033]树上染色(树dp+复杂度分析)

    [题目描述]有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...

  7. 40、JSON数据源综合案例实战

    一.JSON数据源综合案例实战 1.概述 Spark SQL可以自动推断JSON文件的元数据,并且加载其数据,创建一个DataFrame.可以使用SQLContext.read.json()方法,针对 ...

  8. 如何使用git把本地代码上传到github

    Git Bash Here git init git add . git commit -m '说明' git remote add origin https://github.com//.git g ...

  9. C++ 如何进阶?

    1.C++的用途和意义 总体来说,C++作为一门软件开发语言,它的流行度是在减少的.主要原因在于语言的复杂和灵活导致软件开发成本提高,这体现在开发周期和人力上.它不适用于startup公司的快速开发, ...

  10. Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist

    目录 Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist 解决方 ...