题目描述:

Implement int sqrt(int x).

Compute and return the square root of x.

实现开根号,并且返回整数值(这个很重要,不是整数的话就有一种方法用不了了)

方法一:二分法,另外由于我们知道开根号的结果肯定小于等于这个数的二分之一,所以还可以做个线性优化,代码如下:

 class Solution {
public:
int sqrt(int x) {
long long left=;
long long right=x/+;
long long m=(left+right)/;//注意这里有坑
while(left<=right){
m=(left+right)/;
if(m*m>x){
right=m-;
}
else if(m*m==x){
return m;
}
else{
left=m+;
}
}
return right;
}
};

方法二、牛顿迭代法,具体细节百度之,摘录如下:

设r是的根,选取作为r的初始近似值,过点曲线的切线L,L的方程为求出L与x轴交点的横坐标

,称x1为r的一次近似值。过点做曲线的切线,并求该切线与x轴交点的横坐标,称为r的二次近似值。重复以上过程,得r的近似值序列,其中,称为r的次近似值,上式称为牛顿迭代公式

应用到我们的题目里可以得到xi+1= (xi + n/xi) / 2。

于是,代码如下:

 class Solution {
public:
int sqrt(int x) {
if (x == ) return ;
double last = ;
double res = ;
while (res != last)
{
last = res;
res = (res + x / res) / ;
}
return int(res);
}
};

【leetcode】Sqrt(x)的更多相关文章

  1. 【LeetCode】Sqrt(x) (转载)

    Implement int sqrt(int x). Compute and return the square root of x. 原文地址: http://kb.cnblogs.com/page ...

  2. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  3. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  4. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  5. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Linux下多网卡同网段多IP网络分流设定方法

    Linux下多网卡同网段多IP网络分流设定方法 -- :: 标签:Linux下多网卡同网段多IP网络分流设定方法 当服务器需要较高的网络流量时,在其它资源不造成瓶颈的情况下无疑会用到多网卡. 第1选项 ...

  2. Oracle java.sql.SQLException: 数字溢出

    六月 30, 2016 5:47:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinit ...

  3. XML文件的读取----cElementTree

    XML文件如下: <?xml version="1.0" encoding="UTF-8"?> <tokenxml> <token ...

  4. 使用jsvc启动tomcat

    1.在/usr/local/apache-tomcat-7.0.68/bin中有commons-daemon-native.tar.gz  压缩包 2.解压commons-daemon-native. ...

  5. varchar 和 nvarchar 的区别和使用

    区别: 1.nvarchar 不管是一个字符还是一个汉字,都存为2个字节.varchar 汉字是两个字节,其它字符为1个字节. 2.nvarchar(n):包含n个字符的可变长度Unicode字符数据 ...

  6. eclipse 工作环境配置

    1.更换编辑颜色, http://eclipse-color-theme.github.io/update/ 下载离线安装包,解压缩 eclipse-color-theme-update-site\u ...

  7. Pythonj~module

    常数,变量 特殊变量:__xxx__  可以被直接引用 private函数:_xxx __xxx__ 外部不需要引用的函数全部定义成private,只有外部需要引用的函数才定义为public. 导入 ...

  8. jsp页面路径问题

    jsp路径默认不是项目跟路径 一. <%@ page language="java" import="java.util.*" pageEncoding= ...

  9. 微信支付官方.net版之坑你没商量

    最近开始弄支付这块,先是支付宝手机网站支付,也是坑了我许久,不过还好,问题不大. 让我们看看微信支付有多少坑 微信商户平台,你们知道么(我前天才知道,别笑我) 登录地址:https://mch.wei ...

  10. 词频统计_输入到文件_update

    /* 输入文件见337.in.txt 输出文件见338.out.txt */ #include <iostream> #include <cctype> #include &l ...