【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 ...
随机推荐
- 应用HTK搭建语音拨号系统3:创建绑定状态的三音素HMM模型
选自:http://maotong.blog.hexun.com/6261873_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...
- TextMate 小小心得
在Vim.Emacs之间纠结了很久之后,却选择了TextMate P.S. 为何Emacs和Vim被称为两大神器 中文的资料不是很多,一狠心,找了James Edward Gray II的TextMa ...
- show_sync_logs
存入数据库的操作 CREATE TABLE `show_sync_logs` ( `id` ) NOT NULL AUTO_INCREMENT, `queue` ) DEFAULT NULL COMM ...
- python to be linux daemon
所需第三方库:python-daemon[https://pypi.python.org/pypi/python-daemon/] 使用方式: python linux_service.py star ...
- PHP生命周期
2015-08-19 15:05:30 周三 一篇很好的文章 PHP内核探索 总结一下 1. 模块初始化 MINIT 各个PHP模块/扩展初始化内部变量, 告诉PHP调用自己的函数时, 函数体在哪里( ...
- FastReport 使用技巧篇
使用技巧篇 1.FastReport中如果访问报表中的对象? 可以使用FindObject方法. TfrxMemoView(frxReport1.FindObject('memo ...
- fastReport 运行时设计报表 (mtm)
设计报表 通过“TfrxReport.DesignReport”方法调用报表设计器.你必须在你的项目中包含报表设计器 (必要条件是:要么使用“TfrxDesigner”组件,要么增加“frxDesgn ...
- QQ右下角图标不见了
[QQ]我的qq是在线的,但是桌面右下角的企鹅小图标却不见了??? 最好的办法是:CTRL+ALT+Z,先把QQ的控制面板调出来 然后点菜单,选设置,系统设置.在基本设置的选项卡中,窗口设置的最后一项 ...
- 【Android Studio错误】 If you are behind an HTTP proxy, please configure the proxy settings either in IDE or Gradle.
解决办法:以管理员身份运行cmd窗口,输入命令“netsh winsock reset” netsh winsock reset命令,作用是重置 Winsock 目录.如果一台机器上的Winsock协 ...
- oracle触发器加条件判断、dblink
--新增基站同步给电池组信息 create or replace trigger a_b_test after insert or update or delete on BJLT.BASESTATI ...