Implement int sqrt(int x).

Compute and return the square root of x.

注意: 计算平方的时候可能会溢出,所以mid要定义为long

另外,二分法初始上限不可能超过n/2+1

class Solution {
public:
int mySqrt(int x) { int left = ;
int right = x/+; while (left <= right)
{
long mid = left + (right - left) / ;
if (mid * mid <= x && (mid + ) * (mid + ) > x)
{
return mid;
}
else if (mid * mid < x)
left = mid + ;
else
right = mid - ;
} return -;
}
};

69. Sqrt(x) (Divide-and-Conquer)的更多相关文章

  1. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  2. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  3. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  4. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  5. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  6. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  7. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  8. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  9. The Divide and Conquer Approach - 归并排序

    The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...

  10. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

随机推荐

  1. ffmpeg && nginx hls

    备注: 使用ffmpeg 将视频转换为hls,并使用nginx 提供服务,实现点播功能,没有使用rtmp 插件 1.  ffmpeg 命令生成文件 a. ts ffmpeg -y -i mydemo. ...

  2. SharePoint无法搜索解决

    重启服务器后,站点搜索时提示错误,无法进行搜索,进入管理中心搜索管理看到,"查询处理"出错. 解决方法: 停止搜索服务,重新启动,如下图所示 重启服务后,过了几分钟重新查询,查询正 ...

  3. 【转】redis GEO地理位置

    redis目前已经到了3.2版本,3.2版本里面新增的一个功能就是对GEO(地理位置)的支持. 地理位置大概提供了6个命令,分别为: GEOADD GEODIST GEOHASH GEOPOS GEO ...

  4. 【备忘录】CentOS服务器mysql忘记root密码恢复

    mysql的root忘记,现无法操作数据库 停止mysql服务service  mysql stop 然后使用如下的参数启动mysql, --skip-grant-tables会跳过mysql的授权 ...

  5. winform下实现pictureBox全屏播放

    最近开发一个项目,需要通过双击pictureBox实现全屏的功能,网上查找资料,加上一点摸索,最终实现了.做一下记录,以备以后需要. 主要功能都在下面这个类里面 using System; using ...

  6. 查看Google Cloud的IP地址段

    for LINE in `dig txt _cloud-netblocks.googleusercontent.com +short | tr " " "\n" ...

  7. erlang的一些小技巧(不定期更新)

    在任意节点热更新代码 rpc:call(Node,c,l,[Mod]) c和l的指的是code,library Erlang Shell隐藏的小技巧 f(). %%把所有绑定变量释放掉 f(Val). ...

  8. 关于FFT提速

    前面的文章,我们对用硬件实现FFT做了简单介绍.前面文章我们使用的是控制器方式实现FFT,也就是说将一组数据放入FFT模块的RAM中,计算一次蝶形计算,完成后从RAM中读出数据继续计算. 以2048点 ...

  9. cf Double Happiness(判断是否为素数且为4k+1型)

    2790. Double Happiness   time limit per test 3 seconds memory limit per test 128 megabytes input sta ...

  10. bzoj4598: [Sdoi2016]模式字符串

    Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有 ...