【 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 ...
随机推荐
- <Android 基础(一)> Service
介绍 Service(服务)是一个没有用户界面的在后台运行执行耗时操作的应用组件.其他应用组件能够启动Service,并且当用户切换到另外的应用场景,Service将持续在后台运行.另外,一个组件能够 ...
- static int a
static int a只被本文件可见,外部文件不可见;而int a如果在外部文件作以下声明: extern int a,那么它在声明的文件里也是可见的 详见:http://bbs.csdn.net/ ...
- Azure School,让系统化学习回归一站式的简单体验
承认吧,「终身制学习」已经成为一个不可抵挡的趋势.不管你从事什么行业,几乎已经没有什么可以一直吃老本就能搞定的事情,总有各种新的技术和概念等着你去学.至于发展速度飞快的IT 技术,不断的学习更是贯彻始 ...
- 谷歌chrome://chrome-urls/
查看DNS解析时间 1 chrome://dns/ 查看DNS解析的地址 1 chrome://net-internals/#dns 更多功能请参考 1 chrome://chrome-urls/ 以 ...
- SAP Cloud for Customer Extensibility的设计与实现
今天的文章来自Jerry的同事,SAP成都研究院C4C开发团队的开发人员徐欢(Xu Boris).徐欢就坐我左手边的位置,因此我工作中但凡遇到C4C的技术问题,一扭头就可以请教他了,非常方便.下图是他 ...
- objective C 内存管理及属性方法具体解释
oc为每一个对象提供一个内部计数器.这个计数器跟踪对象的引用计数,当对象被创建或拷贝时.引用计数为1.每次保持对象时,调用retain接口.引用计数加1.假设不需要这个对象时调用release,引用计 ...
- linux用命令行运行matlab的.mat文件
入m文件所在目录后,运行 $ matlab -nodesktop -nosplash -r matlabfile 只用文件名matlabfile,不能添加.m
- 深入理解new String()
一. 引言 new String("hello")这样的创建方式,到底创建了几个String对象? 二. 分析 String s1 = "HelloWorld" ...
- Angular 2 树节点的上下移动问题
最近在做一个树节点的上下移动然后实现排序的问题.直接看图: 实现已选查询条件的上下移动.结合了primeng 的picklist 组件. 下面是html代码 <p-tabPanel header ...
- [vijos1066]弱弱的战壕
描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒 ...