原题网址:http://www.lintcode.com/zh-cn/problem/sqrtx/

实现 int sqrt(int x) 函数,计算并返回 x 的平方根。

您在真实的面试中是否遇到过这个题?

Yes
样例

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

挑战

O(log(x))

标签

 
 #include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <algorithm>
using namespace std; int sqrt(int x)
{
if (x<)
{
return -;
} long long low=,high=x,mid=(low+high)/; //为何用int会出错?;
while (low<=high) //mid*mid有可能超出int范围被截断,小于x,若(mid+1)*(mid+1)被截断后仍小于x,返回错误结果;
{
if (mid*mid==x)
{
return mid;
}
else if (mid*mid>x)
{
high=mid-;
}
else
{
if ((mid+)*(mid+)>x)
{
return mid;
}
else
low=mid+;
}
mid=(low+high)/;
} }

参考:

https://blog.csdn.net/gao1440156051/article/details/49766733

http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html

https://www.cnblogs.com/libaoquan/p/7224644.html

141 x的平方根的更多相关文章

  1. 九章lintcode作业题

    1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...

  2. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  3. 141. Sqrt(x)【牛顿迭代法求平方根 by java】

    Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...

  4. 牛顿法求平方根 scala

    你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...

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

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

  6. hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***

    不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...

  7. csuoj 1114: 平方根大搜索

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1114 1114: 平方根大搜索 Time Limit: 5 Sec  Memory Limit:  ...

  8. ocp 1Z0-051 141题---感觉有问题

    141. View the Exhibit and examine the structure of CUSTOMERS and GRADES tables. You need to display ...

  9. leetcode 141

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

随机推荐

  1. 文本聚合函数(wm_concat, listagg, group_concat, string_agg)

    实现目标   1.聚合文本   2.聚合文本(去重)   3.聚合文本(去重),按照指定字段排序   4.聚合文本(去重),按照指定字段排序,替换默认逗号分隔符 MySQL: group_concat ...

  2. 移动端布局 + iscroll + 滚动事件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  3. 在Vue项目中,添加的背景图片在服务器上不显示,如何处理

    遇到的问题: 在vue项目开发过程中,我们常常会在页面中添加背景图片.可是当我们在样式中添加了背景图片,编译打包部署到服务器上时,发现图片并不能显示出来,这是为什么呢~~~ 我们一般写的css样式如下 ...

  4. leetcode-685-冗余连接②

    题目描述: 参考后提交:并查集: class Solution: def findRedundantDirectedConnection(self, edges: List[List[int]]) - ...

  5. 【JZOJ3294】【BZOJ4417】【luoguP3990】超级跳马

    description analysis 矩阵乘法好题 最朴素的\(10pts\)的\(f[i][j]\)容易\(DP\),但是是\(O(nm^2)\)的复杂度 于是把\(10\)分的\(DP\)写出 ...

  6. nextJS使用注意事项

    项目参考 nextJs-yicha 1. 采用方案 create-next-app.antd (1)安装 npx create-next-app --example with-ant-design m ...

  7. lua的运算符

    1.赋值运算符 --赋值 str="helllo".."world" print(str) a,b=10,20 print(a,b) c,d,e=1,2 pri ...

  8. NOIp2018集训test-9-23

    这个NOI模拟题怕是比你们的NOIp模拟题要简单哦.. 友好的生物 应该是一道简单题,但是机房只有辉神一个人想到正解似乎. 被我kd-tree水过去了(这不是kd-tree的裸题吗???(不是)) / ...

  9. weblux上传图片

    我是接口接收图片然后上传到阿里云上,由于引入的是spring weblux,所以使用方式不同,代码如下 @PostMapping(value = "/upload", consum ...

  10. NX二次开发-UFUN计时函数UF_begin_timer

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //计时开始 UF_timer_t Timer ...