原题地址: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的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  2. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  3. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  4. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  5. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  6. [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 ...

  7. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  8. [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 ...

  9. [LeetCode]题解(python):112 Path Sum

    题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...

随机推荐

  1. c#浏览器 遇到的一些问题

    禁用弹窗是需要引用一个新的dll,引用方式http://www.itjsxx.com/csharp/IHTMLDocument2_namespace.html, 禁用新的弹窗的方式http://blo ...

  2. JavaScript学习方法

    首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我们”.您先别急着骂,先听我说. 你叫一个大学生去教小学数 ...

  3. Slickflow.NET 开源工作流引擎高级开发(三) -- 并行分支容器与会签工作流模式的组合

    前言:  流程引擎的核心功能是负责解析流程定义XML和流转,业务环节的不断积累,让人们不断总结和抽象出一些模式,这些模式统称为工作流模式(Workflow Pattern).本文的重点就是介绍一种常见 ...

  4. Sublime Text 2 快捷键(转)

    文件 File 新建文件 Ctrl + N 打开文件 Ctrl + O 打开最近关闭的文件 Ctrl + Shift + T 保存 Ctrl + S 另存为… Ctrl + Shift + S 关闭文 ...

  5. C#远程调用技术WebService修炼手册

    一.课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上.所以在本次分享课开课之 ...

  6. poj 2429 GCD &amp; LCM Inverse 【java】+【数学】

    GCD & LCM Inverse Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9928   Accepted:  ...

  7. delphiredisclient开源GIT

    delphiredisclient - Redis client for Delphi Delphi Redis Client版本2(此分支)与Delphi 10.1 Berlin兼容,更好.警告!如 ...

  8. 添加类似navigationController自带的返回按钮

    添加类似navigationController自带的返回按钮,效果如下: 一.UINavigationcontroller自带的navigationBar 是无法添加左箭头的返回按钮的 在网上搜索了 ...

  9. iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer

    首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...

  10. C#编程(五十七)----------位数组

    位数组 如果需要处理很多位,就可以使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System. ...