文档章节

374. Guess Number Higher or Lower

初雪之音
 初雪之音
发布于 2017/02/26 11:45
字数 182
阅读 8
收藏 1

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.

思路:二分查找法(基础)

/* 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 result = -1;

		if (n >= 1) {
			int low = 1, hight = n;
			while (low <= hight) {
				int mid = (hight - low) / 2 + low;
				if (guess(mid) == -1) {
					hight = mid - 1;
				} else if (guess(mid) == 1) {
					low = mid + 1;
				} else {
					result = mid;
					break;
				}
			}
		}

		return result;
	}
}

 

© 著作权归作者所有

共有 人打赏支持
上一篇: 400. Nth Digit
初雪之音
粉丝 47
博文 268
码字总数 150009
作品 0
广州
程序员
私信 提问
猜测数值大了或者小了 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 whe......

叶枫啦啦
2017/08/15
0
0
Python脚本之流程控制if语句

#!/usr/bin/env python # FileName: if.py number = 23 guess = int(raw_input('Enter an integer :')) if guess == number: print 'Congratulations ,you guessed it.' print 'But you do n......

mastters
2014/08/20
0
0
猜数游戏(求保证能赢的最少钱数)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 whe......

叶枫啦啦
2017/12/28
0
0
LeetCode:Guess Number Higher or Lower

思路: 采用二分法进行查找: 可以调用guess 的API, 在 GuessGame中定义了 guess(int num) 方法,该方法可能返回 -1,1,0, 0表示猜对了,1表示比那个数大,-1表示比那个数小;...

tsmyk0715
2016/09/01
10
0
决战Leetcode: easy part(51-96)

本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明。 欢迎互相交流! email: tomqianmaple@...

qq_32690999
02/09
0
0

没有更多内容

加载失败,请刷新页面

加载更多

程序中设置MySQL的默认值

import com.alibaba.fastjson.JSON;import java.beans.PropertyDescriptor;import java.lang.annotation.*;import java.lang.reflect.Field;import java.lang.reflect.Method;impo......

laolin23
21分钟前
2
0
WordPress没有上级目录的写权限

sudo chmod -R 777 wordpress/wp-content

临江仙卜算子
29分钟前
4
0
大数据学习之大数据技术笔记—spring入门

篇一 spring介绍 spring.io 官网 快速开始 Aop 面向切面编程,可以任何位置,并且可以细致到方法上 连接框架与框架 Spring 就是 IOC AOP 思想 有效的组织中间层对象一般都是切入 service 层 ...

董黎明
30分钟前
5
0
ASP.NET Core MVC 静态文件配置

在启动文件中添加以下配置 public class Startup{ public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddDirectoryBrowser(); ......

whltian
58分钟前
2
0
linux之自定义命令

本人使用的是ubuntu系统,不喜欢建各种桌面快捷链接,但是每次启动个软件,去查找又麻烦,所以自定义了命令,来快捷的启动应用: 1、修改/etc/bash.bashrc,在文件末尾,加上如下List-1中的内...

克虏伯
今天
8
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部