【 Sqrt(x) 】cpp
题目:
Implement int sqrt(int x).
Compute and return the square root of x.
代码:
class Solution {
public:
int mySqrt(int x)
{
if (x<) return x;
int l = ;
int r = x/;
while (l<=r)
{
int mid = (l+r)/;
if ( x / mid < mid )
{
r = mid - ;
}
else if ( x / mid > mid )
{
l = mid + ;
}
else
{
return mid;
}
}
return l-;
}
};
tips:
这个题一开始拿到感觉怪怪的,直接看的solution。
比如输入400,记录mid的结果如下:
100
50
25
12
18
21
19
20
这种虽然产生了震荡,但是震荡必然越来越小,而且每次变化的长度至少是上一次的一半,时间复杂度也确实是O(logn).
有个细节,如果没有整数的平方数,最后会推出循环;而退出循环时,一定是从l - r = 1,因此取l-1返回即可。
=====================================================
第一次过的时候有误区,binary search本来就是震荡的过程,第二次过按照第一次的代码写了一遍。
class Solution {
public:
int mySqrt(int x) {
if ( x< ) return x;
int l = ;
int r = x/;
while ( l<=r )
{
int mid = (l+r)/;
if ( x/mid == mid )
{
return mid;
}
else if ( x/mid > mid )
{
l = mid+;
}
else
{
r = mid-;
}
}
return l-;
}
};
【 Sqrt(x) 】cpp的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- [转] Adobe acrobat 破解教程
最新版的Adobe Acrobat DC Pro可以使我们更方便的管理和编辑PDF文档,现在我为大家带来Adobe Acrobat DC Pro安装及破解教程,供大家安装和使用. 工具/原料 Ad ...
- javascript设计模式之装饰者模式
/* * 装饰者模式提供比继承更有弹性的替代方案 * 在不改变原构造函数的情况下,添加新的属性或功能 */ //需要装饰的类(函数) function Macbook() { this.cost = ...
- 【作业留存】根据IATF框架,设计的一种中小型企业安全拓扑
- 送H-1B 及其他I-129 申请别忘用新表
(梁勇律师事务所,lianglaw.com专稿)移民局从2010年11月23日 更新了申请H-1B 及其他非移民工作签证I-129 表,从2010年12月23日以后收到的I-129表都必须是2010年 ...
- netbackup :nbu备份 Hyper-V 遇到快照错误(状态码 156)
遇到快照错误(状态码 156) 下表介绍与 NetBackup 状态码 156 有关的 Hyper-V 问题. 表:状态码 156 的可能原因 状态码 156 的原因 说明及推荐操作 NetBacku ...
- 使用U盘引导安装CentOS
一.制作linux引导盘 1. 格式化U盘:格式成FAT32格式 2. 安装syslinux https://www.kernel.org/pub/linux/utils/boot/syslinux/ ...
- js字符串的使用
Javascript的内置功能之一就是字符串连接,如果+号用于两个字符串连接 var s="hello,world" //想要查找给定位置的字符 s.cha ...
- 【Django】Django中datetime的处理(strftime/strptime)
strftime<将date,datetime,timezone.now()类型处理转化为字符串类型> strftime()函数是用来格式化一个日期.日期时间和时间的函数,支持date.d ...
- MultipartFile 动态决定是否上传文件,解决不上传文件报错
controller 接收参数 用 HttpServletRequest 代替 @RequestParam() 接收参数 picFile 前台 传文件的参数名字 , 这样 前段 传 nul ...
- 三十一、MySQL 及 SQL 注入
MySQL 及 SQL 注入 如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本章节将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入 ...