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. gradle asciidoc 使用

    备注:    次文档参考github 例子   1.环境准备 node npm (yarn) java KindleGen 备注: 具体的安装可以参考网上相关文档,KindleGen 下载地址:htt ...

  2. 浅谈ecmall插件机制

    插件是独立于原系统的程序模块,目的是在不修改原程序的情况下对系统进行扩展,便于修改和管理.目前web开发中大多是使用钩子形式来定义插件, 比较典型的有 wordpress, drupal系统 ecma ...

  3. 解决openoffice进程异常退出的办法:

    实现以守护进程,定时检测openoffice是否退出,如果进程不存在,通过脚本将openoffice起起来即可.   具体操作步骤: 第一步: 将openoffice.sh脚本放置在root目录下面, ...

  4. 4.JMeter聚合报告分析

    1.Label:每个Jmeter的element的Name值 2.Samples:发出的请求数量 3.Average:平均响应时间 4.Median:表示50%用户的响应时间 5.90%Line:90 ...

  5. 类的声明与实例化及构造方法析构方法(PHP学习)

    <?php class human{ public static $leg=2; public $name = 'leo'; public $age = '25'; public functio ...

  6. js 理解闭包

    学习Javascript闭包(Closure) 引用: 阮一峰 http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures. ...

  7. Linux性能监控 - CPU、Memory、IO、Network

    一.CPU 良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70%. 上下文 ...

  8. windows下mysql定时备份

    场景:一套B/S小系统,租用了一个虚拟服务器windows 2003,数据库是mysql,做每天的数据库备份 1.关于windows下的定时任务执行     * 命令说明 - /sc 指定计划类型,取 ...

  9. STL sort

    STL的sort()算法,数据量大时采用Quick Sort,分段递归排序,一旦分段后的数据量小于某个门槛,为避免Quick Sort的递归调用带来过大的额外负荷,就改用Insertion Sort. ...

  10. HttpClient基础用法

    一.HttpClient HttpClient是Apache HttpComponents 下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包(httpclient-4. ...