JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

special

牛顿迭代法

 1 public class Solution {
 2     public int sqrt(int x) {
 3         // IMPORTANT: Please reset any member data you declared, as
 4         // the same Solution instance will be reused for each test case.
 5         if(x <= 0){
 6             return x;
 7         }
 8         double last = 0;
 9         double cur = x;
10         while(Math.abs(cur - last) > 0.0001){
11             last = cur;
12             cur = (cur + x / cur) / 2;
13         }
14         return (int)cur;
15     }
16 }

 

posted on 2013-11-19 03:53  JasonChang  阅读(180)  评论(0编辑  收藏  举报