1.题目描述

 

Implement int sqrt(int x).

 

Compute and return the square root of x.

2.解法分析

很明显,用二分搜索可解,但是需要防止溢出,所以中间结果和上界下界都要用long long 来保存。

class Solution {

public:

    int sqrt(int x) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if(x<0)return -1;

        if(x<4)return x>0?1:0;

        long long  rmin=0;

        long long  rmax=x/2;

        

        long long  rmid;

        

        while(rmin<rmax)

        {

            rmid=(rmin+rmax)/2;

            if((rmid*rmid)==x)return rmid;

            if((rmid*rmid)<x)rmin=rmid+1;

            else rmax=rmid-1;

        }

        

        int result=rmin;

        while(result*result>x)result--;

        

        return result;

        

    }

};

leetcode—sqrt的更多相关文章

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

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

  2. [leetcode]Sqrt(x) @ Python

    原题地址:https://oj.leetcode.com/problems/sqrtx/ 题意: Implement int sqrt(int x). Compute and return the s ...

  3. Leetcode Sqrt(x)

    参考Babylonian method  (x0  越接近S的平方根越好) class Solution { public: int sqrt(double x) { ) ; , tolerance ...

  4. LeetCode: Sqrt

    Title: Implement int sqrt(int x). Compute and return the square root of x. 思路:这个平方根肯定是在[1,x]之间,所以在这个 ...

  5. LeetCode:Sqrt(x) 解题报告

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. SOLUTION 1: 参见:二分法总结,以及模 ...

  6. LeetCode——Sqrt(x)

    Description: Implement int sqrt(int x). Compute and return the square root of x. 好好学习数学还是非常有用的,牛顿迭代法 ...

  7. [Leetcode] sqrt 开根号

    Implementint sqrt(int x). Compute and return the square root of x. 题意:求根号下x 的值 思路:使用二分搜索.先定义x平方根的取值区 ...

  8. [LeetCode] Sqrt(x) 二分搜索

    Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Math Binary Search     ...

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)

    1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...

  2. 发送消息执行记事本的“另存为”菜单功能(通过WM_COMMAND控制使用别的程序的菜单命令)

    发送消息执行记事本的“另存为”菜单功能procedure TForm1.FormCreate(Sender: TObject);var hNotepad: Cardinal;begin hNotepa ...

  3. C# MySql分页存储过程的应用

    存储过程: 获取范围内的数据 DELIMITER $$ DROP PROCEDURE IF EXISTS `studb`.`GetRecordAsPage` $$ ),), ),)) BEGIN de ...

  4. 设置UITableView section间距

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...

  5. C# 写入XML文档三种方法详细介绍

      三个类将同样的xml内容写入文档,介绍了如何使用XmlDocument类对XML进行操作,以及如何使用LINQ to XML对XML进行操作. 它们分别使用了XmlDocument类和XDocum ...

  6. BZOJ 1004 Cards(Burnside引理+DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1004 题意:三种颜色的扑克牌各有Sr,Sb,Sg张.给出m种置换.两种染色方案在某种置换 ...

  7. c# webbrowser 随机点击链接

    HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...

  8. 使用stringstream时的清空操作

    在C++中可以使用stringstream来很方便的进行类型转换,字符串串接,不过注意重复使用同一个stringstream对象时要先继续清空,而清空很容易想到是clear方法,而在stringstr ...

  9. poj 1067 取石子游戏( 威佐夫博奕)

    题目:http://poj.org/problem?id=1067 题意:有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的 ...

  10. BootStrap基本样式

    文本对齐风格:.text-left:左对齐.text-center:居中对齐.text-right:右对齐.text-justify:两端对齐 取消列表符号:.list-unstyled内联列表:.l ...