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. Delphi语言获得生命的原因和过程

    都说Anders Hejlsberg是Delphi语言的作者,前一阵仔细读了VCL源码,惊叹于它的巧夺天工,未免对编译器的作者有些不服气,觉得首功不是他.今天仔细想了想,还是觉得不服不行.以下是我的理 ...

  2. 57. Insert Interval

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  3. 54. Spiral Matrix

    题目: Given a matrix of m x n elements (m rows, ncolumns), return all elements of the matrix in spiral ...

  4. NSArray 利用数组创建数组

    NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];         ...

  5. Android Navigation Drawer,自定义ActionBar(标题居中)

    整个示例都是改造自 Google Android Training 中的 NavigationDrawer 示例(http://developer.android.com/training/imple ...

  6. url、href、src 详解

    发现自己居然没把url.href.src关系及使用搞清楚,今天就理一下.主要包括:url.src.href定义以及使用区别.顺便试下在segmentfault来一发. URL(Uniform Reso ...

  7. IOS打包脚本

    1.xcodebuild clean -project $projectname.xcodeproj -configuration Release -alltargets2.xcodebuild ar ...

  8. 面试题_76_to_81_Java 最佳实践的面试问题

    包含 Java 中各个部分的最佳实践,如集合,字符串,IO,多线程,错误和异常处理,设计模式等等. 76)Java 中,编写多线程程序的时候你会遵循哪些最佳实践?(答案)这是我在写Java 并发程序的 ...

  9. BZOJ_1629_[Usaco2007_Demo]_Cow_Acrobats_(贪心)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1629 \(n\)头牛叠罗汉.第\(i\)头牛的力量为\(s_i\),重量为\(w_i\),危险 ...

  10. asp.net实现GZip压缩和GZip解压

    最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...