一天一道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. java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet

    解决此异常的方法是: 删除您添加在Referenced Libraries 下的catalina.jar包, 然后删除Webapp下的部署程序,重新部署后一切正常. ? --------------- ...

  2. Linux服务器搭建相关教程链接整理

    Linux: Linux 教程 | 菜鸟教程 linux下如何添加一个用户并且让用户获得root权限 - !canfly - 博客园 Git: 在 Linux 下搭建 Git 服务器 - 黄棣-dee ...

  3. Node.js TTY

    稳定性: 2 - 不稳定 tty 模块包含 tty.ReadStream 和 tty.WriteStream 类.多数情况下,你不必直接使用这个模块. 当 node 检测到自己正运行于 TTY 上下文 ...

  4. mysql substr() 函数

    mysql substr() 函数 用法:substr(string string,num start,num length); string为字符串:start为起始位置:length为长度. 注意 ...

  5. JavaScript switch 语句

    switch 语句用于基于不同的条件来执行不同的动作. JavaScript switch 语句 请使用 switch 语句来选择要执行的多个代码块之一.你可以在JavaScript编程实战中了解怎么 ...

  6. Python3 解释器

    Linux/Unix的系统上,Python解释器通常被安装在 /usr/local/bin/python3.4 这样的有效路径(目录)里. 我们可以将路径 /usr/local/bin 添加到您的Li ...

  7. mongo 读分析

    分布式读 读冲突 分布式中数据库有多份数据,各份数据可能存在不一致性. mongo 只会写到primary节点上,理论上来说不会有文档冲突,也就是说数据库中的数据都以primary节点为标准. 但是有 ...

  8. 剑指Offer——知识点储备--Linux基本命令+Makefile

    剑指Offer--知识点储备–Linux基本命令 1.linux下查看进程占用cpu的情况(top): 格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 主要参 ...

  9. 剑指Offer——知识点储备-网络基础

    剑指Offer--知识点储备-网络基础 计算机网络 http和https的区别 (1)http是http协议运行在tcp之上,所传输的内容都是明文,客户端和服务器端都无法验证对方的身份. (2)htt ...

  10. 联想G510 在新的SSD上安装Win8.1系统,启动的时候自己加载机械硬盘的Win8.1系统

    进入BIOS,选择Boot,将Boot Priority(优先),修改为Legacy(传统) First: 启动的时候就不会使用UEFI First的windows Boot Manager(wind ...