【leetcode】Sqrt(x)
题目描述:
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)的更多相关文章
- 【LeetCode】Sqrt(x) (转载)
Implement int sqrt(int x). Compute and return the square root of x. 原文地址: http://kb.cnblogs.com/page ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】319. Bulb Switcher 解题报告(Python)
[LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- Oracle 与 entity framework 6 的配置,文档
官方文档: http://docs.oracle.com/cd/E56485_01/win.121/e55744/intro001.htm#ODPNT123 Oracle 对 微软 实体框架 EF6 ...
- 快速搭建VPN服务器
http://www.ttlsa.com/linux/centos-install-pptp-vpn/
- c++数据类型和定义
我们都知道,刚开始学习数学的时候.乘法口诀.99乘法口诀.这个是大家都需要背的.背熟了这个,大家才能知道遇到算术题如何计算.这个99乘法口诀就是一种定义. 同样任何的语言都会有很多的定义.比如语文:各 ...
- mybatis xml 大于 小于
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...
- linux grep 查找字符串
2015年8月27日 12:04:58 在当前文件夹查找 public function abc() grep -re 'public function abc\b' . // 可以不加e, 适合函数 ...
- Enum:Game of Lines(POJ 3668)
画直线 题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条 数据量比较少,直接暴力枚举,然后放到set查找即可 #include <iostream> #include < ...
- Spring mvc 文件上传到文件夹(转载+心得)
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- tp5中的一些小方法
// 当使用一个新页面替换当前页面的body后,body刷新了,所选择的select值就不能保存住,解决方法如下: 作业题目<select> <option>--请选择--&l ...
- css样式设置图片半透明度,兼容IE8,火狐
关于背景颜色透明的兼容浏览器的问题,一直是个问题,我所写的兼容IE8,和火狐,说是兼容所有浏览器我就没有测试,有兴趣的朋友可以自己测试下吧. background-color:white;filter ...
- Windows Form 中快捷键设置
在Windows Form程序中使用带下划线的快捷键只需要进行设置: 就能够工作.