您的位置:首页 > 其它

题目 :x的平方根

2015-08-19 18:35 253 查看
实现
int sqrt(int x)
函数,计算并返回 x 的平方根。

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

Yes

哪家公司问你的这个题?
Airbnb
Alibaba
Amazon Apple
Baidu Bloomberg
Cisco Dropbox
Ebay Facebook
Google Hulu
Intel Linkedin
Microsoft NetEase
Nvidia Oracle
Pinterest Snapchat
Tencent Twitter
Uber Xiaomi
Yahoo Yelp
Zenefits
感谢您的反馈

样例

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

挑战

O(log(x))

标签 Expand

二分法

数学

相关题目 Expand

1
(binary-search),(array)
容易 二分查找 29 %

class Solution {

/**

* @param x: An integer

* @return: The sqrt of x

*/

public int sqrt(int x) {

// write your code here

double r = 1.00;

if(x<0) return -1;

if(x==1) return 1;

while(Math.abs(r*r-x)>0.01){

r=(r+x/r)/2;

}

return (int)r;

}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: