原题网址: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. 笔记40 Spring Web Flow——订单流程(构建订单)

    二.订单子流程 在识别完顾客之后,主流程的下一件事情就是确定他们想要什么类型 的披萨.订单子流程就是用于提示用户创建披萨并将其放入订单中 的,如下图所示. showOrder状态位于订单子流程的中心位 ...

  2. easyui datagrid 绑定从后台得到的复杂的特殊数据结构

    由于项目需要,从后台得到的数据统一为了类似{state:xxx,data:xxx,message:xxx}类型 但是easyui datagrid却只认{total:xxx,rows:xxx}...所 ...

  3. Tomcat服务器优化(内存,并发连接数,缓存)

    a) 内存优化:主要是对Tomcat启动参数进行优化,我们可以在Tomcat启动脚本中修改它的最大内存数等等.b) 线程数优化:Tomcat的并发连接参数,主要在Tomcat配置文件中server.x ...

  4. echats问题

    echats 横轴显示不下datazoom配置,加入滚动条 实例博客  https://blog.csdn.net/Zheng_xiao_xin/article/details/80882113 常用 ...

  5. 关于ajax请求status 200 却进入error 回调函数或显示跨域问题的解决方案及原因

    这虽然不是前端的问题吧,但如果遇到那种不靠谱的后台 还是可以拿来打脸的 转:https://segmentfault.com/a/1190000012469713

  6. Keepalived+LVS+nginx搭建nginx高可用集群

    1. 环境准备 1. VMware; 2. 4台CentOs7虚拟主机:192.168.122.248,192.168.122.68, 192.168.122.110, 192.168.122.167 ...

  7. leetcood学习笔记-167-两数之和 II - 输入有序数组

    题目描述: 第一次提交: class Solution(object): def twoSum(self, numbers, target): """ :type num ...

  8. MLE极大似然估计和EM最大期望算法

    机器学习十大算法之一:EM算法.能评得上十大之一,让人听起来觉得挺NB的.什么是NB啊,我们一般说某个人很NB,是因为他能解决一些别人解决不了的问题.神为什么是神,因为神能做很多人做不了的事.那么EM ...

  9. [Nowcoder] 数数字

    题意:...咕咕懒得写了. 思路: 裸的记搜... #include <bits/stdc++.h> using namespace std; #define ll long long m ...

  10. [JZOJ 5817] 抄代码

    题意: 给定2T个串,带修的判断两个串是否按规则一样?? 思路: 两个串是"抄袭的"肯定就是: 1.长度一样. 2.特殊字符位置一样 3.对于每个\(x\)在两个串中出现位置一样, ...