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; } }
实现 int sqrt(int x) 函数,计算并返回 x 的平方根。您在真实的面试中是否遇到过这个题?Yes哪家公司问你的这个题?AirbnbAlibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Micros