题目描述:

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. python——常用功能之文本处理

    前言 在生活.工作中,python一直都是一个好帮手.在python的众多功能中,我觉得文本处理是最常用的.下面是平常使用中的一些总结.环境是python 3.3 0. 基础 在python中,使用s ...

  2. NET程序内存分析工具CLRProfiler的使用(性能测试)

    http://blog.csdn.net/wy3552128/article/details/8158938 大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随 ...

  3. NFV技术中遇到的新名词

    NUMA topology:Non-Uniform Memory Access (NUMA) is a computer system architecture that is used with m ...

  4. 【GoLang】tcmalloc && jemalloc

    https://www.douban.com/note/512625720/ http://blog.csdn.net/hanxin1987216/article/details/8156010 ht ...

  5. springboot 整合Redis

    0.导入 maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  6. [转]Java连接各种数据库的方法

    //MySQL:       String Driver="com.mysql.jdbc.Driver";   //驱动程序     String URL="jdbc:m ...

  7. 如何准确高效的获取数据库新插入数据的主键id

    例如我们新建了一张表UserInformation,字段如下Id,为主键,自增,其它字段Name,Pwd,Email 然后我们来执行一个新增插入操作: insert into UserInformat ...

  8. pycharm 中 pep8 检查开启.

    pycharm pep8检查的开启,默认是暗黄色,我这里为了醒目给改成了黄色.

  9. 抓取网页内容生成kindle电子书

    参考: http://calibre-ebook.com/download_linux http://blog.codinglabs.org/articles/convert-html-to-kind ...

  10. irssi忽略退出,加入消息

    IRSSI: IGNORE JOINS, PARTS, QUITS AND NICKS MESSAGES I use IRC on a daily basis and my client of cho ...