leetcode--Sqrt(x)

来源:互联网 发布:学网络编程 编辑:程序博客网 时间:2024/06/05 15:49

Implement int sqrt(int x).

Compute and return the square root of x.

[java] view plain copy
  1. public class Solution {  
  2.     public int mySqrt(int x) {  
  3.         double pre = 999;  
  4.         double next = 0;  
  5.         while(Math.abs(pre-next)>=0.00001){  
  6.             next = pre;  
  7.             pre = x/(2*pre)+pre/2.0;              
  8.         }  
  9.         return (int)pre;  
  10.     }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46410085

原创粉丝点击