LeetCode(69) Sqrt(x)
题目
Total Accepted: 67411 Total Submissions: 286086 Difficulty: Medium
Implement int sqrt(int x).
Compute and return the square root of x.
分析
不适用库函数实现求根。
该题目一种解法是利用二分的思想,要注意的问题便是计算溢出问题,数据类型应该采用unsigned long long ;
另一种解法是 牛顿迭代法,思想参考百度百科 以及参考博客
为了方便理解,就先以本题为例:
计算x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,如左图所示。
首先取x0,如果x0不是解,做一个经过(x0,f(x0))这个点的切线,与x轴的交点为x1。
同样的道理,如果x1不是解,做一个经过(x1,f(x1))这个点的切线,与x轴的交点为x2。
以此类推。
以这样的方式得到的xi会无限趋近于f(x)=0的解。
判断xi是否是f(x)=0的解有两种方法:
一是直接计算f(xi)的值判断是否为0,二是判断前后两个解xi和xi-1是否无限接近。
经过(xi, f(xi))这个点的切线方程为f(x) = f(xi) + f’(xi)(x - xi),其中f’(x)为f(x)的导数,本题中为2x。令切线方程等于0,即可求出xi+1=xi - f(xi) / f’(xi)。
继续化简,xi+1=xi - (xi2 - n) / (2xi) = xi - xi / 2 + n / (2xi) = xi / 2 + n / 2xi = (xi + n/xi) / 2。
AC代码
class Solution {
public:
int mySqrt(int x) {
if (x < 0)
return -1;
//使用二分法求解
unsigned long long lhs = 0, rhs = (x + 1) / 2;
while (lhs <= rhs)
{
unsigned long long mid = (lhs + rhs) / 2;
//注意溢出问题,使用无符号长整型存储临时乘积
unsigned long long tmp1 = mid * mid;
if (tmp1 == x)
{
return mid;
}
else if (tmp1 < x)
{
lhs = mid + 1;
}
else{
rhs = mid - 1;
}//else
}//while
unsigned long long tmp = lhs * lhs;
if (tmp <= x)
return lhs;
else
return rhs;
}
};
LeetCode(69) Sqrt(x)的更多相关文章
- LeetCode(69):x 的平方根
Easy! 题目描述: 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...
- Qt 学习之路 2(69):进程
Qt 学习之路 2(69):进程 豆子 2013年11月9日 Qt 学习之路 2 15条评论 进程是操作系统的基础之一.一个进程可以认为是一个正在执行的程序.我们可以把进程当做计算机运行时的一个基础单 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
随机推荐
- SQL SUM函数内使用CASE函数
- 实例 - 在这个表里进行查询: 查询出如下结果(统计每天的输赢次数): - 开始查询 - 首先创建测试表: CREATE TABLE info( date ), result ) ); 插入测试数 ...
- 学习JavaScript数据结构与算法 (一)
学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...
- SPRING-BOOT系列之Spring4深入分析
上篇 : SPRING-BOOT系列之Spring4快速入门 1. 假如我们有这样一个场景,在一个组件中想获取到容器对象,那么我们也可以使用Autowired来完成装配.那么我们还可以让类集成一个接口 ...
- 165 Compare Version Numbers 比较版本号
比较两个版本号 version1 和 version2.如果 version1 大于 version2 返回 1,如果 version1 小于 version2 返回 -1, 除此以外 返回 0.您可 ...
- P2658 汽车拉力比赛
题目描述 博艾市将要举行一场汽车拉力比赛. 赛场凹凸不平,所以被描述为M*N的网格来表示海拔高度(1≤ M,N ≤500),每个单元格的海拔范围在0到10^9之间. 其中一些单元格被定义为路标.组织者 ...
- SQL系列函数——字符串函数
1.charindex函数用来寻找一个指定的字符(串)在另一个字符串中的起始位置,返回一个整数,没找到就返回0. select CHARINDEX('SQL','Microsoft SQL SERVE ...
- Android手机屏幕投射到电脑神器Vysor
做android开发的,经常要把手机屏幕投射到电脑,用来演示.普遍的解决方案是360或者豌豆荚的演示功能,缺点是延迟非常厉害,大概有3秒左右,非常影响演示效果.以下介绍Vysor,几乎0延迟,能与手机 ...
- kde framework概述(KDE Framework译文)
KDE Frameworks 基于QT框架,提供简单实用的类(例如那些KCoreAddons里的类)去为桌面应用的日常需要整合出解决方案(例如KNewStuff用于在应用中获取可下载的附加内容,或者那 ...
- java语言基础-类型运算细节
代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...
- centos7环境搭建Eureka-Server注册中心集群
目的:测试和线上使用这套独立的Eureka-Server注册中心集群,目前3台虚拟机集群,后续可直接修改配置文件进行新增或减少集群机器. 系统环境: Centos7x64 java8+(JDK1.8+ ...