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. Python Requests IP直连

    import re import requests from urllib3.util import connection _orig_create_connection = connection.c ...

  2. Python数据类型(列表)

    文章内容参考了教程:http://www.runoob.com/python/python-basic-syntax.html#commentform Python 列表(List) 序列是Pytho ...

  3. RESTful简单介绍

    1 什么是restful Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制. 资源:互联网所有的事物都 ...

  4. 关于table-layout的用法

    定义:tableLayout 属性用来显示表格单元格.行.列的算法规则. 自动表格布局:auto(默认) 在自动表格布局中,列的宽度是由列单元格中没有折行的最宽的内容设定的. 此算法有时会较慢,这是由 ...

  5. WPF的ComboBox简单用法

    1. ComboBox:下拉列表框 效果如下: 2.通常用法是 显示内容 + 选中内容后获得的值(也就是 Name = Value的键值对) 故以键值对来定义一个类,如: public class C ...

  6. java中的各种命令参数

    java中有很多命令参数,这些命令参数有些是控制jvm行为的,有的则是供应用程序使用.我所了解的参数主要有三种,现在说一说这三种类型的参数. (1)命令行参数. 命令行参数就是类似与c语言的命令行参数 ...

  7. CentOS7 防火墙操作

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  8. c++ mysql connector 学习汇总

    1)MySQL Connector/C++入门教程(上) 里面有autoCommit的用法 2)国外的一篇文章

  9. hdu1385 最短路字典序

    http://blog.csdn.net/ice_crazy/article/details/7785111 http://blog.csdn.net/shuangde800/article/deta ...

  10. Java并发编程:深入剖析ThreadLocal (总结)

    ThreadLocal好处 Java并发编程的艺术解释好处是:get和set方法的调用可以不用在同一个方法或者同一个类中. 问答形式总结: 1. ThreadLocal类的作用 ThreadLocal ...