Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

解法一:牛顿迭代法

求n的平方根,即求f(x)=x2-n的零点

设初始值为x0,注,不要设为0,以免出现除数为0,见后。

则过(x0,f(x0))点的切线为g(x)=f(x0)+f'(x0)*(x-x0)

g(x)与x轴的交点为x1=x0-f(x0)/f'(x0)

递推关系为xn+1=xn-f(xn)/f'(xn)

当收敛时即为解。

class Solution {
public:
int sqrt(int x) {
double x0 = ;
double x_next = -(x0*x0 - x)/(*x0) + x0;
while(fabs(x0-x_next) > 0.00001)
{
x0 = x_next;
x_next = -(x0*x0 - x)/(*x0) + x0;
}
return x0;
}
};

解法二:二分法

注意返回为int,结果会取整。

class Solution {
public:
int sqrt(int x) {
long long low = ;
long long high = x;
long long mid;
while(low <= high)
{
mid = (low+high)/;
long long result = mid*mid;
if(result == x)
return mid;
else if(result > x)
high = mid-;
else
low = mid+;
}
return high;
}
};

【LeetCode】69. Sqrt(x) (2 solutions)的更多相关文章

  1. 【LeetCode】69. Sqrt(x) 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:库函数 方法二:牛顿法 方法三:二分查找 日 ...

  2. 【一天一道LeetCode】#69. Sqrt(x)

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...

  3. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  4. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  5. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  6. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  7. 【LEETCODE】69、动态规划,easy,medium级别,题目:198、139、221

    package y2019.Algorithm.dynamicprogramming.easy; /** * @ProjectName: cutter-point * @Package: y2019. ...

  8. 【LeetCode】338. Counting Bits (2 solutions)

    Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num  ...

  9. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

随机推荐

  1. 使用STL中的list容器实现单链表的操作

    #include<iostream> #include<list> #include<algorithm> using namespace std; void Pr ...

  2. 设置启用mysql慢查询日志

    --设置log文件位置 set global slow_query_log_file = /sql_log/slow_log.log; --设置是否启用记录没有使用索引的sql set global ...

  3. js复制兼容:ZeroClipboard复制到剪切板(支持IE、FF、Chrome)

    注意:ZeroClipboard在本地测试无法直接使用,必须在服务器上测试,如http://localhost... 准备:ZeroClipboard.swf 和 ZeroClipboard.js 小 ...

  4. linux automake 交叉编译

    . ├── aclocal.m4 ├── autoscan.log ├── config.log ├── config.status ├── configure ├── configure.in ├─ ...

  5. thymleaf th:text 和 th:utext 之间的区别

    1 th:text属性 可对表达式或变量求值,并将结果显示在其被包含的 html 标签体内替换原有html文本 文本连接:用“+”符号,若是变量表达式也可以用“|”符号 e.g. 若home.welc ...

  6. Robot Framework 安装及环境配置

    Robot Framework 安装及环境配置 Robot Framework 介绍 Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以 ...

  7. 【Hibernate步步为营】--hql查询小介

    HQL 是指Hibernate Query Language,它是Hibernate的查询语言,拥有一套自己的查询机制,它的查询语句和SQL非常类似.在使用的时候可以非常快上手.HQL提供了基本上SQ ...

  8. 写的一个split函数

    vector<string> strsplit(const string& str) { vector<string> vec; string sstr1=str, s ...

  9. 运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy),三大件(bigthree problem)

    一般的我们喜欢这样对对象赋值: Person p1;Person p2=p1; classT object(another_object), or    A a(b); classT object = ...

  10. Linux下Anaconda的安装使用与卸载及问题解决

    1. 安装 到官网下载对应的版本文件:Download Anaconda Now! 下载完之后,在终端输入: bash 下载好的文件 整个过程点几下回车就好了.但是到最后一步,会提示是否把anacon ...