(二分查找 拓展) 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 ...
随机推荐
- vue.js的手脚架vue-cli项目搭建的步骤
手脚架是什么? 众所周知,现在的前端项目发展得越渐越大,我们前端程序员要从0开始去搭建一套完整的项目很费时,所以这时候前端工程的手脚架就出现了. 我用得vue-cli也是其中之一,还有其他的我也说不清 ...
- MySQL5.7参数log_timestamps
最近测试MySQL 5.7.21 Community Server这个版本的MySQL数据库时,发现其错误日志的时间跟系统当前时间不一致,后面检查发现日期时间格式都是UTC时间,查了一下相关资料,原 ...
- SQL Server实际执行计划COST"欺骗"案例
有个系统,昨天Support人员发布了相关升级脚本后,今天发现系统中有个功能不能正常使用了,直接报超时了(Timeout expired)的错误.定位到相关相关存储过程后,然后在优化分析的过程中,又遇 ...
- Xamarin 开发过的那些项目
您可能已经看到类似的统计数据:智能手机用户在手机媒体上花费了89%的时间使用应用程序.或者听说Gartner预测到2017年移动应用程序下载将产生价值770亿美元的收入.很难不考虑这些数字.今天,每个 ...
- C#与SQL Server数据库连接
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【原】Java学习笔记009 - 阶段测试
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 1.需求:打印如下 ...
- Cs231n课堂内容记录-Lecture 6 神经网络训练
Lecture 6 Training Neural Networks 课堂笔记参见:https://zhuanlan.zhihu.com/p/22038289?refer=intelligentun ...
- 英语口语练习系列-C05-水电
<登幽州台歌>·陈子昂 陈子昂(公元659-公元700年),唐代文学家,初唐诗文革新人物之一. Num 诗句 1 前不见古人, 2 后不见来者. 3 念天地之悠悠, 4 独怆然而涕下! T ...
- .NET CORE学习笔记系列(2)——依赖注入【1】控制反转IOC
原文:https://www.cnblogs.com/artech/p/net-core-di-01.html 一.流程控制的反转 IoC的全名Inverse of Control,翻译成中文就是“控 ...
- syso快捷键设置
syso快捷键