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)的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【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 ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 题解:二分的方法,从0,1,2.....x搜索sqrt(x)的值 ...

  6. 【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 ...

  7. 【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 ...

  8. 【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 ...

  9. 【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 ...

  10. 【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 ...

随机推荐

  1. 线程局部存储空间 pthread_key_t、__thread 即 ThreadLocal

    https://www.jianshu.com/p/495ea7ce649b?utm_source=oschina-app 该博客还未学习完  还有   pthread_key_t    Thread ...

  2. SQL Server触发器创建、删除、修改、查看

    一:触发器是一种特殊的存储过程,它不能被显式地调用,而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约束. 二:SQL Server为每个触发器都创 ...

  3. JAVA学习6:用Maven创建Spring3 MVC项目

    一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 eclipse-jee-juno-SR2-win32 二.      ...

  4. JS框架设计之模块加载系统

    任何语言一到大规模应用阶段,必然要拆封模块,有利于维护和团队协作,与Java走得最近的dojo率先引进了加载器,使用document.write与同步Ajax请求实现,后台dojo以JSONP的方法来 ...

  5. Oracle给Select结果集加锁,Skip Locked(跳过加锁行获得可以加锁的结果集)

    1.通过select for update或select for update wait或select for update nowait给数据集加锁 具体实现参考select for update和 ...

  6. 【随笔】win7下安装Apache服务器

    1.首先下载64位的apache服务器httpd-2.4.10-win64.zip 2.解压,可以是默认路径,也可以自定义路径,我这里是E:/ 3.打开E:/Apache24/conf文件夹 4.随便 ...

  7. bzoj 2164: 采矿

    Description 浩浩荡荡的cg大军发现了一座矿产资源极其丰富的城市,他们打算在这座城市实施新的采矿战略.这个城市可以看成一棵有n个节点的有根树,我们把每个节点用1到n的整数编号.为了方便起见, ...

  8. ajax上传数据

    ---恢复内容开始--- ajax上传数据,(简洁版) 1.上传普通同表单标签内容. 1.获取表单的内容 1. var file=$('#file').val();(放在点击事件后面) 2. var ...

  9. 前端小结(5)---- iframe

    iframe对应的div: <div id="iframezone"> <iframe id="iframe" frameborder='0' ...

  10. window7下搭建本机版git并在本机上进行代码管理

    1.安装软件:Git-1.9.2-preview20140411.exe与TortoiseGit-1.8.8.0-64bit.msi 2.在本机任一目录下执行如下操作: 3.创建一个test.java ...