LeetCode_sqrt(x)
class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write int main() function if(x < 0 ) return -1; if(x == 0) return 0; if(x >= 1 && x <= 3) return 1; double start = 2.0; double next = 0.5 *(start + x/start); while(abs(start * start - x) > 0.000001){ start = next; next = 0.5 *(start + x/start); } return int(start); } };
两种方法的详细分析: http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html
--------------------------------------------------------------------天道酬勤!