题目描述:

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. BOM基础部分

    打开.关闭窗口 •open –蓝色理想运行代码功能 •close –关闭时提示问题   常用属性 •window.navigator.userAgent •window.location   窗口尺寸 ...

  2. mysql的innodb中事务日志ib_logfile

    mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...

  3. ACM/ICPC 之 SPFA-兑换货币(POJ1860)

    //水题-SPFA解法 //套汇是指兑换货币后能使本金上升 //给定本金货币编号,货币间的汇率和手续费,求能否套汇成功 //Time:16Ms Memory:200K #include<iost ...

  4. ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

    求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...

  5. Java类变量、实例变量的初始化顺序

    题目: public class InitTest{ public static int k = 0; public static InitTest t1 = new InitTest("t ...

  6. nyoj298_点的变换_错误

    点的变换 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 平面上有不超过10000个点,坐标都是已知的,现在可能对所有的点做以下几种操作: 平移一定距离(M),相对X ...

  7. 【leetcode】 Unique Path ||(easy)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. js正则匹配以固定格式结尾的字符串并匹配是手机访问,则跳转

    <script> //var pcUrl = "http://res.meadin.com/HotelData/98986_1.shtml"; var pcUrl = ...

  10. JNDI 和JDBC的区别

    1.JNDI 和JDBC的区别和联系.两者都是API,是一个标准.并不是什么产品或方法.JDBC 全称:Java Database Connectivity 以一种统一的方式来对各种各样的数据库进行存 ...