【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 ...
随机推荐
- 那些H5用到的技术(4)——弹幕
前言思路实现模式无限循环模式时间线模式停止显示弹幕 前言 以前玩卷轴射击游戏的时候,大量的BOSS子弹让我们无路可逃的时候,让我见识到了真正弹幕的威力,可自从A站B站火了之后,大量评论留言参与到了视频 ...
- Respone笔记
1 设置定时刷新的头 //设置定时刷新的头 response.setHeader("refresh", "5;url=http://www.baidu.com" ...
- h5空白页面过渡加载
h5空白页面过渡加载 页面第一部分内容是图片,考虑到手机图片加载慢,想用简单.转化为base64的图片过渡 开始尝试将图片转为灰度图片,结果还是很大. 后来选取重要元素,保存2位的png,尺寸是494 ...
- VirtualBox 虚拟机磁盘空间不够用,增大空间方法(这里以MAC为例)
开始在Virtualbox 上,安装MAC系统的时候只分配了20G的空间,随着Xcode 开发软件安装的东西多了,比如:IOS 的Simulator 的各种版本,4.3,5.0,6.0 加起来要到少要 ...
- 初学spring-boot
部分转载自:http://blog.java1234.com 一.简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架 ...
- java java-web linux C C++ 安装系统 数据结构 算法 教程分享
qq1206630874 v18455730857 免费分享
- C运算符总结
算术运算符 加减 + - 自左向右 +-同级 乘除取模 * % / 自左向右 高于+ - 自加自减 ++ -- 右结合性 高于基本算术运算符 正负 -+ 自右向左 跟++ --同级 赋值运算符 赋值 ...
- bzoj 2167: 公交车站
Description Z市交通不发达,所有公交路线覆盖的边竟然一个环也不包含,甚至该市的公交路线有可能会分为几个互不连通的块,这可真是不可思议.有一天,你突然听到一条消息,说你的M个同学被困在了Z市 ...
- java 记录
1.spring ide 的安装图解 https://blog.csdn.net/u012369373/article/details/55097380 2.ssm框架配置内容 http://www. ...
- IDEA创建Maven项目和子模块
一.新建Project a. 新建项目,点击Create New Project b. 选择使用maven来新建项目 选择新建Maven模块(对IDEA来说是项目) 如果Project SDK那里没有 ...