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 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 already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num): class Solution(object):
def guessNumber(self, n):
"""
:type n: int
:rtype: int
"""
left, right = 1, n
while(left <= right):
mid = (left + right) / 2
res = guess(mid)
if res == 0:
return mid
elif res == -1:
right = mid -1
elif res == 1:
left = mid + 1 return -1
Python [Leetcode 374]Guess Number Higher or Lower的更多相关文章
- leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- [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 ...
- 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 ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- [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 ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- nodejs fs学习
在本章的开始,我本来只想写一些关于fs模块的内容,虽然这个模块包含的方法非常的多,但没有想到的是它和我们上一篇文章Node.js Buffer还存在着关联,所以我又把关于Buffer的内容简单的学习下 ...
- nor flash的一般操作与分析
是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR Flash 技术,彻底改变了原先由EPROM(Electrically Programmable Read-Only-Me ...
- 介绍Web项目中用到的几款JQuery消息提示插件
第一款 noty 官方网站:https://github.com/needim/noty 第二款 artDialog artDialog是一个精巧的web对话框组件,压缩后只有十多KB,并且不依赖其他 ...
- 在两台服务器之间建立信任关系解决scp,ssh等不用输入密码等问题
A服务器(client)向B服务(server)SCP,SSH. A服务器:ssh-keygen -t rsa -C "kangzj" 一直回车. cd .ssh vim id_r ...
- zabbix api
#!/usr/bin/env python # -*-coding:utf-8 -*- import requests import json class AutoZabbix: def __init ...
- Linux系统下修改IP地址、网关、DNS的基本方法
临时修改IP地址.网关.主机名.DNS,马上生效,无需重启(重启后失效) 1.修改主机名 #hostname Slyar 2.修改IP地址(eth0为网卡名称) #ifconfig eth0 192. ...
- 使用Idea 配置maven
使用Idea 配置maven 使用idea 配置maven有4步: (1)下载Idea,maven (2)安装idea, (3)设置maven的环境变量, (4)具体的配置过程 当前环境: 操作系统: ...
- struts2——文件下载自定义文件名,包括中文
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- CommStringLib
#include "syswatcher/CommStringLib.h" //#include "String16.h" #undef LOG_TAG #de ...
- CocoaPods学习系列4——进阶用法
这篇文章,记录一下CocoaPods的进阶用法. 进阶用法主要体现在.podspec文件和Podfile的配置上. .podspec文件的进阶配置 以官方的一个.podspec文件示例细说: Pod: ...