[leetcode]Sqrt(x) @ Python
原题地址:https://oj.leetcode.com/problems/sqrtx/
题意:
Implement int sqrt(int x)
.
Compute and return the square root of x.
解题思路:实现开平方函数。这里要注意的一点是返回的时一个整数。通过这一点我们可以看出,很有可能是使用二分查找来解决问题的。这里要注意折半查找起点和终点的设置。起点i=1;终点j=x/2+1;因为在(x/2+1)^2 > x,所以我们将折半查找的终点设为x/2+1。
代码:
class Solution:
# @param x, an integer
# @return an integer
def sqrt(self, x):
if x == 0:
return 0
i = 1; j = x / 2 + 1
while( i <= j ):
center = ( i + j ) / 2
if center ** 2 == x:
return center
elif center ** 2 > x:
j = center - 1
else:
i = center + 1
return j
[leetcode]Sqrt(x) @ Python的更多相关文章
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- [LeetCode]题解(python):113 Path Sum II
题目来源 https://leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
随机推荐
- Android-Selector不起作用
Android-Selector不起作用 Overview 今天在做项目的时候,使用了一些 Selector 来给ImageView设置不同的Drawable,但是无论怎么设置ImageView的属性 ...
- android 滑动冲突
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 通过move事件的 拦截. 在滑动组件中,重写 在拦截触摸事件的时候 这个方法, 然后 ...
- Scanner和BufferedReader的区别和用法
在命令行模式下要输入数据至程序中时,我们可以使用标准输入串对象System.in.但是,我们并不经常直接使用它,因为System.in提供的 read方法每次只能读取一个字节的数据,而我们平时所应用的 ...
- Java 操纵XML之创建XML文件
Java 操纵XML之创建XML文件 一.JAVA DOM PARSER DOM interfaces The DOM defines several Java interfaces. Here ar ...
- 正睿OI 提高 Day1T3 ZYB玩字符串(DP)
题目链接 设可能的答案串为p,长为len.p一定是s的一个子串且len|n. 虽然一些p在s中可能被断成若干段,但删掉其中的若干段后,这段区间一定会被全部消掉. 于是枚举p后,可以用f[i][j]表示 ...
- java集合系列之LinkList
概要 第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...
- OS X - 在80端口启动Nginx
不知道你是怎么在你的mac上安装nginx的,但是如果你跟我一样: brew install nginx 然后你会发现你的nginx.conf中的端口是8080. 于是你可能像我一样试着把端口改为80 ...
- allowDefinition='MachineToApplication错误
配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误信息: 在应用程序级别之外使用注册为 allowDefinition='Mac ...
- 【教程】新手如何制作简单MAD和AMV,学不会那都是时辰
[教程]新手如何制作简单MAD和AMV,学不会那都是时 http://tieba.baidu.com/p/2303522172 [菜鸟教你做MAD]Vegas制作MAD入门教程 http://tieb ...
- WCF中的REST是什么
基于SOAP消息格式的WCF之所以强大原因之一是因为SOAP消息头的高度扩展性.相应的WS-*协议很多都体现在消息头封装的信息上,包括诸如寻址,需要调用方法名,维护Session的信息等等…… SOA ...