【Leetcode】【Medium】Sqrt(x)
Implement int sqrt(int x)
.
Compute and return the square root of x.
解题思路1,o(log(n)):
像这种从初始遍历查找匹配的任务,往往可以使用二分法逐渐缩小范围;
初始从0~x/2 + 1,然后逐渐以二分思想缩小查找范围。
解题思路2:
牛顿迭代法(百度百科)
一些小优化:
1、不需要等到Ni * Ni 无限接近于x时,再确定Ni是返回值。
根据牛顿迭代法图解发现,Ni+1 和 Ni不断迭代求解过程中,差距越来越小。
当int(Ni+1) == int(Ni)时,说明迭代结果永远会处于int(Ni+1)和int(Ni+1)+1之间;
因此,由于转换类型自动向下取整,就已经可以确定int(Ni+1)是要求的返回值;
2、初始不必设置为N0 = X,可以从N0 = X/2+1,开始迭代。注意保持N0*N0 > X,否则不仅增加了迭代次数,并且不利于编程;
class Solution {
public:
int mySqrt(int x) {
double ans = x / + ;
int pre = int(ans);
while (ans * ans > x) {
ans = x / ( * ans) + ans / ;
if (pre == int(ans))
break;
else
pre = ans;
}
return pre;
}
};
其他注意点:
1、往往对于int间求值,多考虑int越界问题;
2、遍历查找,多考虑二分的方法;
【Leetcode】【Medium】Sqrt(x)的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【leetcode刷题笔记】Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x. 题解:二分的方法,从0,1,2.....x搜索sqrt(x)的值 ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum
[Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...
随机推荐
- 下载Kitti 数据集(dataset) data_road.zip
官网下载http://www.cvlibs.net/download.php?file=data_road.zip,耗时近3小时,虽然只有几百兆. 但是,我坚持下来了. 保存到了百度网盘,以供国内用户 ...
- 动态时间规整DTW(Dynamic Time Warping )
动态时间规整DTW(Dynamic Time Warping ) 原文:https://blog.csdn.net/raym0ndkwan/article/details/45614813 算法笔记- ...
- Python 实现flatten功能
from collections import Iterable def flatten(items): for x in items: if isinstance(x, Iterable) and ...
- JS及Dom练习 | 页面滚动文字
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- restsharp 组件调用返回 gbk 编码的api,中文乱码解决方法。(restsharp response 中文乱码 gbk)
最近要调一个restful风格的api 用了 一个开源第三方组件,组件还是蛮好用的, 支持直接按参数定义实体类,然后发起请求之前直接 addobject 的方式就把请求参数给添加进去了, 解码的时候可 ...
- Apache 配置虚拟域名的最简单方式
一.配置httpd.conf: 1.取消Include conf/extra/httpd-vhosts.conf的注释,代码如下: # Virtual hostsInclude conf/extra/ ...
- <数据挖掘导论>读书笔记3--分类
1.分类的基本概念 分类任务就是通过学习得到一个目标函数f,把每个属性集x映射到一个预先定义的类标号y 目标函数也称为分类模型. 2. 解决分类问题的一般方法: 决策树分类法 基于规则的分类法 神经网 ...
- C#之RabbitMQ系列(一)
RabbitMQ–环境搭建 MQ MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接 ...
- PLC编程逻辑思路
PLC编程逻辑思路 在整个执行过程的流程中,都是在不断地找启动条件,停止条件以及输出结果.当条件不够时,就得想办法如果添加标志位,根据已有条件去构造条件:当结果开发耦合时,就制造中间继电器去除耦合. ...
- webservice log4net日志写入失败
原因1:如果webservice和调用者都部署在一台机器上,日志有可能写到了项目所在目录中,虽然你添加的服务引用是部署在iis下的,但不会写到这.暂时解决办法,把webservice部署到内网服务器上 ...