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. 如何在CentOS系统中安装配置SNMP服务

    CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,现在有一大部分服务器在使用此操作系统:SNMP(简单网络 ...

  2. 【转】Linux export 命令

    原文网址:http://mymobile.iteye.com/blog/1407601 Linux export 命令 功能说明: 设置或显示环境变量.(比如我们要用一个命令,但这个命令的执行文件不在 ...

  3. 什么是spark(五)Spark SQL

       Spark SQL Spark SQL主要分为两部分,一部分是Spark Sql在scala中直接,使用作为执行层面上的应用,本质上就是生成DAG的另外一种形式:其发生试下Driver中生成: ...

  4. error: device not found

    C:\Users\Administrator>adb shell error: device not found    出现上面情况,首先检查设备管理器中,安卓的驱动是否安装OK?   如果驱动 ...

  5. 排序 第K大等问题总结

    在公司面试时,当场写排序比较多,虽然都是老掉牙的问题,还是要好好准备下 快速排序,以第一个元素为关键词比较,每次比较结束,关键词都会去到最终位置上 //7 3 2 9 8 3 4 6 //7 3 2 ...

  6. dos命令行连接操作ORACLE数据库

    C:\Adminstrator> sqlplus "/as sysdba" 查看是否连接到数据库 SQL> select status from v$instance; ...

  7. java代码---------再练习ChatAt()的用法

    总结: 没有理解方法的含义.瞎用 package com.mmm; //实现字符串中某个字符出现的次数 public class Mo { public static void main(String ...

  8. Apache Ignite简介以及Ignite和Coherence、Gemfire、Redis等的比较

    一.Ignite简介 Apache Ignite 内存数组组织框架是一个高性能.集成和分布式的内存计算和事务平台,用于大规模的数据集处理,比传统的基于磁盘或闪存的技术具有更高的性能,同时他还为应用和不 ...

  9. ZOJ 3593 One Person Game(拓展欧几里得求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  10. 【UVALive】3029 City Game(悬线法)

    题目 传送门:QWQ 分析 以前见到过差不多的这题. xhk说是单调栈水题,但我又不会单调栈,于是当时就放下了. 这么久过去了我还是不会用单调栈做这题,用的是悬线法. 非常好写 代码 #include ...