【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 ...
随机推荐
- c/c++ int long float double 表示范围
引自https://blog.csdn.net/xuexiacm/article/details/8122267
- 在ionic3+angular4项目中添加自定义图标
在阿里图标库下载自己所需要的图标解压为一下目录 把iconfont.xx文件全部放到src/assets/fonts/文件夹下,可以全部替换里面的文件,但是要把之前iconfont.css文件下的文件 ...
- (转)MySQL- 5.7 sys schema笔记,mysql-schema
原文:http://www.bkjia.com/Mysql/1222405.html http://www.ywnds.com/?p=5045 performance_schema提供监控策略及大量监 ...
- Robot Framework_Ride(Run标签)
前言 我一直在想 Robot Framework 不要 RIDE 可不可以.对于编写测试用例来说,只要掌握 RobotFramework 的语法规则,随便在一个你顺手的编辑器下编写也没问题,甚至效率更 ...
- LinuxShell脚本编程基础4-条件测试与条件判断
1.条件测试(test,[]) #! /bin/bash echo "请输入登陆的用户名:" read name1 if test "$name1" = &qu ...
- 修改eclipse下tomcat的内存大小/解决内存溢出
我们安装完成eclipse之后,在我们的安装目录下有一个名为eclipse.ini文件. 打开文件里面的内容如下: -startup plugins/org.eclipse.equinox.launc ...
- 【Docker】制作一个支持SSH终端登录的镜像
首先从官方或者docker.cn的镜像库中pull下来ubuntu镜像: docker pull ubuntu 现在用命令查看一下pull下来的ubuntu镜像: docker images 关于如何 ...
- Python基础(4) - 变量
Python 命名规则: 变量名必须是字母或者_开头. 变量名的其他部分可以是字母,_或者数字. Python是大小写敏感的. 以下划线开头的标识符是有特殊意义: 以单下划线开头(_foo)的代表不能 ...
- html控件
checkbox val = "<li class='layer'><label><input type='checkbox' checked name='la ...
- [转载+补充][PY3]——环境配置(2)——windows下安装pycharm并连接Linux的python环境
原文地址:<你所会用到的Python学习环境和工具> 1. 下载安装Pycharm专业版 具体方法略.Pycharm5激活方法参考http://www.cnblogs.com/snsdzj ...