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. 同时安装Python2和Python3,如何兼容并切换使用详解

    由于历史原因,Python有两个大的版本分支,Python2和Python3,又由于一些库只支持某个版本分支,所以需要在电脑上同时安装Python2和Python3,因此如何让两个版本的Python兼 ...

  2. unittest简介

    unittest是python里面的单元测试框架 1 unittest 简介 1).先导入 unittest2).用 help 函数查看源码解析3).查看描述:Python unit testing ...

  3. cloudera-scm-server启动时出现Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection问题解决方法(图文详解)

    问题现象 查看 [root@cmbigdata1 cloudera-scm-server]# pwd /var/log/cloudera-scm-server [root@cmbigdata1 clo ...

  4. 在VIM中添加行号的方法

    VIM编辑器是可以显示行号的.但是,有时候我们需要在整个代码的行首添加行号.怎么实现呢?实现的方法有很多,这里就介绍我知道的一种吧. 在每行行首添加某个字符串 :%s/^/your_string/ 在 ...

  5. python web 框架的基本逻辑练习题

    # -*- coding:utf-8 -*- # date : 2017/12/22 from wsgiref.simple_server import make_server # 首先是导入wsgi ...

  6. Java 并发(一) --- CAS

    CAS 原理 先来看看下面的代码是否可以输出预期的值.开启了两个线程,是否会输出200 呢 结果由于并发的原因,结果会小于或等于200 , 原因出现在 count++; 由于这一行代码存在三个操作: ...

  7. .net iis excel导出问题

    碰到几个问题的解决方法 1.当我远程服务器时才可以导出excel!!关闭了远程就不行... 解决:运行mmc -32组件服务 ->DCOM Config->Microsoft Excel  ...

  8. linux创建日期文件名

    linux创建文件名添加当前系统日期时间的方法 使用`date +%y%m%d` Example: mkdir `date +%y%m%d` tar cfvz /tmp/bak.`date +%y%m ...

  9. 十二 Cent OS下 tomcat启动项目响应速度很慢

    在tomcat部署了web项目,每次启动项目都需要花费2-3分钟,甚至有的时候需要花费10分钟左右,实在是太慢了. 在网上查找解决方案,把 jdk/jre/lib/security/java.secu ...

  10. Springmvc file多附件上传 显示 删除操作

    之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...