一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Implement int sqrt(int x).

Compute and return the square root of x.

(二)解题

实现sqrt(x),找到一个数,它的平方等于小于x的最接近x的数。

class Solution {
public:
    int mySqrt(int x) {
        int i = 0 ;
        int j = x
        while(i<j)
        {
            long mid = (i+j)/2 +1;//二分查找取中间值
            if(mid*mid>x) j = mid-1;
            else if(mid*mid<x) i = mid;
            else if(mid*mid==x) return mid;//找到
        }
        return i;//没找到就返回i
    }
};

【一天一道LeetCode】#69. Sqrt(x)的更多相关文章

  1. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  2. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

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

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  4. LeetCode 69. Sqrt(x) (平方根)

    Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...

  5. Leetcode 69. Sqrt(x)

    Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...

  6. (二分查找 拓展) leetcode 69. Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  7. [LeetCode] 69. Sqrt(x)_Easy tag: Binary Search

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  8. Leetcode 69 Sqrt(x) 二分查找(二分答案)

    可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...

  9. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

随机推荐

  1. 中间件——canal小记

    接到个小需求,将mysql的部分数据增量同步到es,但是不仅仅是使用canal而已,整体的流程是mysql>>canal>>flume>>kafka>> ...

  2. Java第5次实验提纲(集合)

    PTA与参考资料 重要参考-集合简述 题集:jmu-Java-05-集合 集合实验文件 第1次实验 1.1 7-1ArrayListIntegerStack(课堂演示) 注1:不要导入java.awt ...

  3. Dynamics CRM2016 Web Api之更新时间字段值

    前篇我们论述了时间字段的查询,本篇来论述下时间字段的更新. 还是以之前建的当地时间(时间行为为用户当地时间)字段来测试 可以看到web api更新的是数据库的时间,而在前台的反映就是做了加8处理,所以 ...

  4. oracle11实战详解

    因为最近可以学习oracle了,所以昨天把oracle装好了,装的时候因为种种不知名的原因,把我的mysql居然连连不上了.说实话如果自己看教程安装的话还是有一点小复杂的,特别是对于我这种学渣来说,我 ...

  5. [code segments] OpenCV3.0 SVM with C++ interface

    talk is cheap, show you the code: /***************************************************************** ...

  6. Compile C++ code in Matlab with OpenCV support

    Provides a function named as "mex_opencv(src)" The code function mex_opencv(src) ARC = 'x6 ...

  7. C++ 中const作用

    一.对const与#define的特点及区别的理解 #define只是用来做文本替换的,#define常量的生命周期止于编译期,它存在于程序的代码段,在实际程序中它只是一个常数,一个命令中的参数,并没 ...

  8. 深入浅出如何解析xml文件---上篇

    xml小伙伴们并不陌生,xml是可扩展标记语言,标准通用标记语言语言的子集,是一种用来标记电子文件使其具有结构性的标记语言.我们知道xml可以用dom与sax等方法进行解析,但是xml为什么要解析呢? ...

  9. 微信小程序基础之创建使用教程

    本文档将带你一步步创建完成一个微信小程序,并可以在手机上体验该小程序的实际效果.这个小程序的首页将会显示欢迎语以及当前用户的微信头像,点击头像,可以在新开的页面中查看当前小程序的启动日志. 1. 获取 ...

  10. SVN与eclipse整合与使用、SVN与Apache整合

    SVN与eclipse整合 下载SVN插件(http://subclipse.tigris.org) http://subclipse.tigris.org/servlets/ProjectDocum ...