1 public class Solution { 2 public int sqrt(int x) { 3 // Start typing your Java solution below 4 // DO NOT write main() function 5 double result = x; 6 7 while (Math.abs(result * result - x) > 0.0001) 8 { 9 result = (result + x / result) / 2; 10 } 11 12 return (int)result; 13 } 14 }
利用牛顿迭代法。可以参考http://blog.csdn.net/wumuzi520/article/details/7026808