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. Android 基于蓝牙的方向控制器

    最近开发了一个蓝牙控制器App,用手机远程控制小球的运动. 包含了一些基础知识:多线程使用,页面UI数据更新,按钮事件,选择项功能,蓝牙功能(蓝牙打开,蓝牙搜索,蓝牙连接,蓝牙命令发送,蓝牙命令接收) ...

  2. ubuntu14.04 内核升级到 linux kernel 4.9

    http://blog.csdn.net/liuruiqun/article/details/55097292

  3. Html基础知识详解

    一定要做的符合客户要求,不是自己认为对的. 一.基础标签 1.1 大小颜色位置 <!DOCTYPE HTML> <html> <head> <meta htt ...

  4. sublime在高分辨率屏幕下,左侧边栏字体问题

    因为新的笔记本是1920*1080分辨率的屏幕,在打开sublime的时候,侧边栏的文件夹名字会变得很小,虽然很清晰,但是太小了看起来很吃力, 如下图所示 网上找了很多的方法试了好久没有成功,一天闲下 ...

  5. 全面的framebuffer详解一

    转:http://blog.chinaunix.net/uid-20628575-id-72534.html 一.FrameBuffer的原理 FrameBuffer 是出现在 2.2.xx 内核当中 ...

  6. spingBoot-自定义过滤器

    package com.cx.springboot.filters; import java.io.IOException; import javax.servlet.Filter; import j ...

  7. Code First 数据库的表中属性的配置

      数据类型的约定配置 默认规则 列的数据类型是由数据库决定的,SqlServer的默认规则如下 String: nvarchar(MAX) Int:int Bool:bit Decimal:deci ...

  8. Kubernetes集群安全概述

    API的访问安全性 API Server的端口和地址 在默认情况下,API Server通过本地端口和安全端口两个不同的HTTP端口,对外提供API服务,其中本地端口是基于HTTP协议的,用于在本机( ...

  9. llvm中如何利用分支概率和基本块频率估计

    1. 背景 llvm自2.9版以后,已经集成了对分支概率和基本块频率的静态分析. 分支概率(branch probability)是指在程序的控制流图中,从控制流从一个基本块A到其任意后继基本块Si的 ...

  10. JSONObject 转换 JSON复杂对象

    Bean定义: public class GetM100DataResponse { private String service;//接口代码 private String sessionId;// ...