69. Sqrt(x)(二分查找)
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)(二分查找)的更多相关文章
- Leetcode 69 Sqrt(x) 二分查找(二分答案)
		
可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...
 - (二分查找 拓展) 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 ...
 - 实现 sqrt(x):二分查找法和牛顿法
		
最近忙里偷闲,每天刷一道 LeetCode 的简单题保持手感,发现简单题虽然很容易 AC,但若去了解其所有的解法,也可学习到不少新的知识点,扩展知识的广度. 创作本文的思路来源于:LeetCode P ...
 - C#LeetCode刷题-二分查找
		
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
 - Leedcode算法专题训练(二分查找)
		
二分查找实现 非常详细的解释,简单但是细节很重要 https://www.cnblogs.com/kyoner/p/11080078.html 正常实现 Input : [1,2,3,4,5] key ...
 - 69. Sqrt(x)  - LeetCode
		
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
 - PHP-----二维数组和二分查找
		
二维数组由行和列组成.由arr[$i][$j]表示,先后表示行和列,类似于坐标点. 打印二维数组-----通过两次遍历,第一次遍历每一行,第二次遍历每一行的具体元素,并且通过使用count($arr[ ...
 - java 13-1 数组高级二分查找
		
查找: 1.基本查找:数组元素无序(从头找到尾) 2.二分查找(折半查找):数组元素有序 pS:数组的元素必须有顺序,从小到大或者从大到小.以下的分析是从小到大的数组 二分查找分析: A:先对数组进行 ...
 - python函数(4):递归函数及二分查找算法
		
人理解循环,神理解递归! 一.递归的定义 def story(): s = """ 从前有个山,山里有座庙,庙里老和尚讲故事, 讲的什么呢? ""& ...
 
随机推荐
- 实现Runnable接口和继承Thread类区别
			
如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享. 实现Runnable接口比继承Thread类所具有的优势: 1):适合多个相同的程序代码的 ...
 - Spring装配Bean的过程
			
首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...
 - 设置jQuery validate插件错误提示位置
			
参照上一篇bootstrap布局注册表单 使用校验插件默认位置显示提示信息,发现错误提示信息换行了,由于增加了提示信息,表单显示高度也增加了,如下 默认提示信息位置代码为 将错误提示设置其显示在右边, ...
 - php截取中文字符串时乱码问题
			
<?php function chinesesubstr($str,$start,$len) { //$str指字符串,$start指字符串的起始位置,$len指字符串长度 $strlen=$s ...
 - 计算时间:一个C++运算符重载示例
			
Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...
 - 网络电话pjsip Getting Started: Building for Apple iPhone, iPad and iPod Touch
			
Getting Started: Building for Apple iPhone, iPad and iPod Touch ¶ Getting Started Preparation Get th ...
 - web基础----->servlet中得到请求的数据
			
对tomcat的源码做一些分析,今天我们就开始servlet中的请求分析. form表单中的默认类型 一.在index.jsp中get请求: <form action="Paramet ...
 - JZOJ.5335【NOIP2017模拟8.24】早苗
			
Description
 - 统计文件中单词的个数---Shell及python版
			
最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...
 - redis缓存数据架构实战
			
redis命令参考:http://redisdoc.com/ 与memcache对比 redis安装配置 yum安装 yum -y install redis 源码安装 PS:make报错**问题:* ...