Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

Example 1:

Input: 4
Output: 2

Example 2:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
  the decimal part is truncated, 2 is returned.
 class Solution {
public int mySqrt(int x) {
long lo = 0;
long hi = x/2+1; while(lo<=hi){
long mid = (hi-lo)/2+lo;
if(mid*mid==x)
return (int)mid;
else if(mid*mid>x)
hi = mid-1;
else
lo = mid+1; }
return (int)lo-1;
}
}

69. Sqrt(x)(二分查找)的更多相关文章

  1. Leetcode 69 Sqrt(x) 二分查找(二分答案)

    可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...

  2. (二分查找 拓展) leetcode 69. Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  3. 实现 sqrt(x):二分查找法和牛顿法

    最近忙里偷闲,每天刷一道 LeetCode 的简单题保持手感,发现简单题虽然很容易 AC,但若去了解其所有的解法,也可学习到不少新的知识点,扩展知识的广度. 创作本文的思路来源于:LeetCode P ...

  4. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  5. Leedcode算法专题训练(二分查找)

    二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...

  6. 69. Sqrt(x) - LeetCode

    Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...

  7. PHP-----二维数组和二分查找

    二维数组由行和列组成.由arr[$i][$j]表示,先后表示行和列,类似于坐标点. 打印二维数组-----通过两次遍历,第一次遍历每一行,第二次遍历每一行的具体元素,并且通过使用count($arr[ ...

  8. java 13-1 数组高级二分查找

    查找: 1.基本查找:数组元素无序(从头找到尾) 2.二分查找(折半查找):数组元素有序 pS:数组的元素必须有顺序,从小到大或者从大到小.以下的分析是从小到大的数组 二分查找分析: A:先对数组进行 ...

  9. python函数(4):递归函数及二分查找算法

    人理解循环,神理解递归!  一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...

随机推荐

  1. Linux上Nginx部署配置

    一.下载软件 openssl-fips-2.0.10.tar.gz pcre-8.40.tar.gz zlib-1.2.11.tar.gz nginx-1.10.2.tar.gz gcc-c++ 下载 ...

  2. docker学习-docker容器

  3. 使用rlwrap调用sqlplus中历史命令

    此文来自http://www.cnblogs.com/mchina/archive/2013/03/08/2934473.html 在此谢谢原创作者. 在Linux中运行SQL*Plus,不能调用历史 ...

  4. Android 使用Spinner实现下拉列表

    课程目标1.了解Spinner下拉列表的使用和功能2.学会使用系统默认的Spinner3.学会使用自定义样式的Spinner 执行步骤第一步:添加一个下拉列表项的list,这里添加的项就是下拉列表的菜 ...

  5. C/C++对Lu系统内置动态对象进行运算符重载

    欢迎访问Lu程序设计 C/C++对Lu系统内置动态对象进行运算符重载 1 说明 要演示本文的例子,你必须下载Lu32脚本系统.本文的例子需要lu32.dll.lu32.lib.C格式的头文件lu32. ...

  6. HTTP/2笔记之流和多路复用

    零.前言 本部分将讲解HTTP/2协议中对流的定义和使用,其实就是在说HTTP/2是若何做到多路复用的. 一.流和多路复用的关系 1. 流的概念 流(Stream),服务器和客户端在HTTP/2连接内 ...

  7. LeetCode——N-Queens

    Description: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that n ...

  8. 几种常用的SQL优化工具及方法

    转自:http://blog.itpub.net/35489/viewspace-764856/ 1. sql 详细执行计划,主要检查驱动路径,索引是否合适:同一个pl/sql窗口连续执行即可:exp ...

  9. mac必装工具以及mac使用介绍

    必装工具 Scroll Reverserhttp://pilotmoon.com/scrollreverser/:一款可以使得鼠标使用方式和windows系统一致的软件 编程工具 ,,,,, 常用快捷 ...

  10. Online handwriting recognition using multi convolution neural networks

    w可以考虑从计算机的“机械性.重复性”特征去设计“低效的”算法. https://www.codeproject.com/articles/523074/webcontrols/ Online han ...