题目,就是实现一个开方,返回是整数。int sqrt(int x)

用二分法,因为一个数的开方肯定小于 x/2 + 1, 因为小于5的某些数的开方并不一定比x/2小,所以要+1,那么们定义一个left一个right分别为0和x/2 + 1,然后更新左右边界,直至左边界大于右边界,返回右边界就是答案。

class Solution {
public:
int sqrt(int x) {
long long left = ;
long long right = x/ + ; while(left <= right)
{
long long tmp = (left + right)/ * ((left + right)/); // 一定要注意括号,后面的括号要是少了就错了,因为会先乘再除2结果就不一样
if (tmp == x) return (left + right)/;
else if (tmp > x)
right = (left+right)/ - ;
else
left = (left+right)/ + ;
}
return right;
}
};

还有种用牛顿迭代法

leetcode[71] Sqrt(x)的更多相关文章

  1. LeetCode 71.简化路径

    LeetCode 71.简化路径 题目描述: 以 Unix 风格给出一个文件的绝对路径,你需要简化它.或者换句话说,将其转换为规范路径.在 Unix 风格的文件系统中,一个点(.)表示当前目录本身:此 ...

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

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

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

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

  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). Compute and return the square root of x, where x is guaranteed to be a no ...

  6. Leetcode 69. Sqrt(x)

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

  7. 【leetcode】Sqrt(x)

    题目描述: Implement int sqrt(int x). Compute and return the square root of x. 实现开根号,并且返回整数值(这个很重要,不是整数的话 ...

  8. Java for LeetCode 069 Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 解题思路一: public int mySqrt(int x) ...

  9. (二分查找 拓展) 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 ...

随机推荐

  1. SpringMVC 学习笔记(两) @RequestMapping、@PathVariable和其他注意事项

    1.1. @RequestMapping映射请求 SpringMVC 采用 @RequestMapping 注解为控制器指定能够处理那些URL 请求 @requestMapping  能够定义在 类  ...

  2. (大数据工程师学习路径)第二步 Vim编辑器----查找替换

    一.字符的替换及撤销(Undo操作) 1.替换和撤销(Undo)命令 替换和Undo命令都是针对普通模式下的操作 命令 说明 r+<待替换字母> 将游标所在字母替换为指定字母 R 连续替换 ...

  3. JavaScript之二:this

    在JavaScript中,this的指代对象是什么?最精辟的解释却只有一句话: when a function of an object was called, the object will be ...

  4. WSHPSRS-匹克选择列表生成器-SRS(R12.2.3)

     匹克选择列表生成器-SRS(R12.2.3) --US Program:Pick Selection List Generation - SRS Short Name:WSHPSRS Appli ...

  5. word插入图片显示不完整的解决的方法

    有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.

  6. Linux:闪光的宝石,智慧(下一个)

    2005年4月7日.Linus Torvalds公布了一款新型通用工具软件包,叫做"Git"(the Git source code management system).&quo ...

  7. 查询记录rs.previous()使用

    查询记录rs.previous()使用 假如查询一个数据表,假设没有记录就显示提示信息.有就所有显示出来查询结果.这时假设是有查询结果的话就须要进行两次查询.第一次查完记录指针指向最后一条记录.開始第 ...

  8. crawler_工具类_RegexUtils_正则帮助类

    package com.cph.crawler.core.utils; import java.util.ArrayList; import java.util.List; import java.u ...

  9. Java 测试并行编程(三)

    有很多其他的交替运行 因为在并行代码中的错误一般是低概率事件.因此,试运行并发差错时需要反复多次,但是,有很多方法可以提高发现这些错误的概率 ,在前面提到的,在多处理器系统.假设 线程的数量,那么 与 ...

  10. Linux svnserver存储路径和文件的详细解释

    svn有两种存储方式:BDB和FSFS,眼下用的最多的是FSFS方式,这样的方式的话.通常是存储在\db\revs目录下,里面有一堆以版本命名的文件.如:0.1.2.3.4......,那个就是了 比 ...