Implement int sqrt(int x).

Compute and return the square root of x.

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

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

  1. class Solution {
  2. public:
  3. int mySqrt(int x) {
  4.  
  5. int left = ;
  6. int right = x/+;
  7.  
  8. while (left <= right)
  9. {
  10. long mid = left + (right - left) / ;
  11. if (mid * mid <= x && (mid + ) * (mid + ) > x)
  12. {
  13. return mid;
  14. }
  15. else if (mid * mid < x)
  16. left = mid + ;
  17. else
  18. right = mid - ;
  19. }
  20.  
  21. return -;
  22. }
  23. };

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. Nchan nginx 支持的开源消息推送模块

    1. 介绍 // 官方说明 Nchan is a scalable, flexible pub/sub server for the modern web, built as a module for ...

  2. kong 安装

    1. yum 参考信息 https://bintray.com/kong/kong-community-edition-rpm $ sudo yum install epel-release $ su ...

  3. bzoj1017(JSOI2008)魔兽地图

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1017 钱数很少,所以它也能压进状态里. 还有向上贡献几个物品.所以状态就是第 i 号物品,向 ...

  4. c# 数据库通用类DbUtility

    DbProviderType数据库类型枚举 /// <summary> /// 数据库类型枚举 /// </summary> public enum DbProviderTyp ...

  5. 使用php生成数字、字母组合验证码(一)

    项目中经常会遇到一些登陆验证,支付验证等等一系列安全验证的策略.实现方法多种多样,下面就来讲解下如何用php生成简单的文字+数字组合的验证码: 所用语言php,gd库 原理解释: a>实质上是在 ...

  6. javascript基础-js对象

    一.js对象的创建 1.普通最简单的方式 var teacher = new Object( ); teacher.name = "zhangsan"; teacher.age = ...

  7. 关于FFT的硬件实现

    DFT在实际应用中非常重要,可以计算信号的频谱,功率谱和线性卷积等. 离散傅里叶变换的公式: 其中:  称为旋转因子. 由欧拉公式可得: 直接按DFT变换进行计算,当序列长度N很大时,计算量非常大,所 ...

  8. SpringMVC-Spring-Hibernate项目搭建之三-- freemarker & 静态资源整合

    一. 前段目录结构如下架构如下: 二. freemarker文件配置 在 web.xml 文件中指定 spring 配置文件的位置 三. 配置springmvc-servlet.xml文件 1)配置自 ...

  9. Java编程打印出1000以内所有的完数

    /*如果一个数等 于其所有因子之和,我们就称这个数为"完数" * 例如6的因子为1,2,3, 6=1+2+3, 6就是一一个完数. * 请编程打印出1000以内所有的完数*/ pu ...

  10. Oracle 10g RAC TAF

    Oracle RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会影 ...