文档章节

374. Guess Number Higher or Lower

初雪之音
 初雪之音
发布于 2017/02/26 11:45
字数 182
阅读 7
收藏 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;
	}
}

 

© 著作权归作者所有

共有 人打赏支持
初雪之音
粉丝 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

没有更多内容

加载失败,请刷新页面

加载更多

Hbase 概述及特点

1、Hbase概述 HBase是一种构建在HDFS之上的分布式、面向列的存储系统。在需要实时读写、随机访问超大规模数据集时,可以使用HBase。 尽管已经有许多数据存储和访问的策略和实现方法,但事实上...

PeakFang-BOK
30分钟前
0
0
TortoiseGit(乌龟git)保存用户名密码的方法

windows下比较比较好用的git客户端有2种: 1. msysgit + TortoiseGit(乌龟git) 2. GitHub for Windows github的windows版也用过一段时间,但还是不太习惯。所以目前仍然青睐与msysgit+乌龟g...

simpower
49分钟前
1
0
Java并发编程:volatile关键字解析

volatile这个关键字可能很多朋友都听说过,或许也都用过。在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果。在Java 5之后,volatile关键字才得以重获生...

engeue
今天
2
0
通过ajax访问远程天气预报服务

http://www.webxml.com.cn/zh_cn/index.aspx 更改wsdl文件 打开文件将15行,51行,101行去掉 然后把文件复制到c盘 然后在桌面上面就生成了文件 将文件打成jar包 package cn.it.ws.weather;...

江戸川
今天
1
0
聊聊storm的tickTuple

序 本文主要研究一下storm的tickTuple 实例 TickWordCountBolt public class TickWordCountBolt extends BaseBasicBolt { private static final Logger LOGGER = LoggerFactory.getLogg......

go4it
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部