(二分查找 拓展) leetcode 69. Sqrt(x)
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.
--------------------------------------------------------------------------------------------
这个问题,其实很简单(用sqrt直接OK了),不过,我用二分查找解决这个题
C++代码:
class Solution {
public:
int mySqrt(int x) {
if(x <= ) return x;
int left = ;
int right = x;
while(left <= right){
int mid = left + (right - left)/;
if(x / mid >= mid){
left = mid + ;
}
else
right = mid - ;
}
return right;
}
};
用sqrt:
class Solution {
public:
int mySqrt(int x) {
if(x <= )return x;
return (int)(sqrt(x));
}
};
(二分查找 拓展) leetcode 69. Sqrt(x)的更多相关文章
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- Leetcode 69 Sqrt(x) 二分查找(二分答案)
可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- [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 ...
- (二分查找 拓展) leetcode278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
随机推荐
- MongoDB 常用的数据备份梳理汇总
1.基于数据文件的备份 直接将原始的数据文件Copy至备份的地方,这个方法的优点是比较快,因为备份和恢复都不需要转换数据格式.缺点就是需要锁住数据库服务器,但是此方案通常备份是在从节点上进行,备份过程 ...
- Python第六天 类型转换
Python第六天 类型转换 目录 Pycharm使用技巧(转载) Python第一天 安装 shell 文件 Python第二天 变量 运算符与表达式 input()与raw_inp ...
- c/c++ 继承与多态 友元与继承
问题1:类B是类A的友元类,类C是类B的友元类,那么类C是类A的友元类吗?函数fun是类B的友元函数,那么fun是类A的友元函数吗? 都不是,友元关系不能传递. 问题2:类B是类A的友元类,类C是类B ...
- IIS 反向代理到 Apache、Tomcat
将请求的网址重写重定向到其它网址.当80端口被占用无法同时使用两个Web服务的解决方案,使得IIS和Apache Tomcat 共存 环境 WindowServer 2008 IIS7 Apache ...
- Error:Cannot run program "svn" (in directory "E:demo\Hello"): CreateProcess error=2,
file-->settings-->version controller --> subversion
- 滑动窗口最大值的golang实现
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位. 返回滑动窗口最大值 输入: nums = [, ...
- 单元测试(qunit)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- PHP奇淫技巧
https://www.jb51.net/list/list_67_1.htm PHP技巧:https://www.jb51.net/list/list_67_13.htm mysql三范式 1NF: ...
- 返回数组中指定的一列,将键值作为元素键名array_column
array_column() 函数 从记录集中取出 last_name 列: <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' => 5 ...
- Jetson TX2(1)ubutu1604--安装Nvidia Linux驱动
https://www.jianshu.com/p/c8ebe4aaa708 系统开机首次进入的是以nvidia用户登录的Ubuntu 命令行界面.Nvidia 驱动安装 通过sudo su 输入密码 ...