[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 ...
随机推荐
- 模拟赛T1 素数
没有链接 描述: 给p,q,求a^2+b^2 = p*q解的个数,p,q是素数 沙雕打表结论题 然后怼了3h吼爆零 题解 首先这是个结论题 然后这是证明 代码 #include <stdio.h ...
- 【BZOJ-3681】Arietta 网络流 + 线段树合并
3681: Arietta Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 182 Solved: 70[Submit][Status][Discuss ...
- How Visual Studio 2012 Avoids Prompts for Source
[原文地址]:http://blogs.msdn.com/b/heaths/archive/2012/07/26/how-visual-studio-2012-avoids-prompts-for-s ...
- STM32 F4 ADC DMA Temperature Sensor
STM32 F4 ADC DMA Temperature Sensor Goal: detecting temperature variations using a temperature senso ...
- [廖雪峰] Git 分支管理(1):创建与合并分支(HEAD、master、dev、指针)
每次提交,Git 都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支.HEAD 严格来说不是指向提交,而是指向 mas ...
- Android: INSTALL_FAILED_UPDATE_INCOMPATIBLE错误解决措施
晚上在测一个widget,前面测的好好的,后面再安装的时候发现如下错误:[2009-06-07 02:39:35 - battery] Performing sync[2009-06-07 02:39 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- 使用newScheduledThreadPool来模拟心跳机制
(使用newScheduledThreadPool来模拟心跳机制) 1 public class HeartBeat { 2 public static void main(String[] args ...
- C#编程(二十八)----------泛型类的功能
泛型类的功能 在创建泛型类时,还需要一些其他的C#关键字.例如,不能把null赋予泛型类型.此时,可以使用default关键字.如果泛型类型不需要object类的功能,但需要调用泛型类上的某些特定方法 ...
- Ioc:Autofac Registration Concepts
Reflection Components When using reflection-based components, Autofac automatically uses the constru ...