【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 ...
随机推荐
- signed char型内存位bit表示
signed char型内存 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9 ...
- LSTM时间序列预测学习
一.文件准备工作 下载好的例程序 二.开始运行 1.在程序所在目录中(chapter_15)打开终端 输入下面的指令运行 python train_lstm.py 此时出现了报错提示没有安装mat ...
- mysql 必知必会总结
以前 mysql 用的不是很多, 2 天看了一遍 mysql 必知必会又复习了一下基础. 200 页的书,很快就能看完, 大部分知识比较基础, 但还是了解了一些以前不知道的知识点.自己做一个备份,随 ...
- Struts中Validate()和validateXxx的使用
Struts中Validate()和validateXxx的使用 学习struts2之后,你会发现validate在之前是没有的!它是怎么实现的呢? validate和validateXxxx都是拦截 ...
- JavaScript Ajax 实现学习
创建异步对象: function createXmlHttp(){ var xhobj=false; try{ xhobj=new ActiveXObject("Msxml2.XMLHTTP ...
- 【Linux】Linux C socket 编程之UDP
发送方: /* * File: main.c * Author: tianshuai * * Created on 2011年11月29日, 下午10:34 * * 主要实现:发送20个文本消息,然后 ...
- web项目 log4j2 指定配置文件路径
pom.xml需要额外引入的jar: <dependency> <groupId>org.apache.logging.log4j</groupId> <ar ...
- hosts文件配置参数介绍
hosts文件配置参数介绍 1, ansible_ssh_host : 指定主机别名对应的真实 IP,如:100 ansible_ssh_host=192.168.1.100,随后连接该主机无须指定完 ...
- 【idea快捷键】
IntelliJ Idea 常用快捷键列表 idea也是可以切换到eclipse风格的快捷键方式的 在keymap中切换即可 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表 ...
- [转].Net Core Web应用发布至IIS后报“An error occurred while starting the application”错误
本文转自:http://www.cnblogs.com/TomGui/p/6438686.html An error occurred while starting the application. ...