题目:

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的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. 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~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Android 超简单的拖动按钮 悬浮按钮 吸附按钮

    第一种    第二种    第一种实现方法 xml布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...

  2. Mysql数据库操作语句总结(二)

    Mysql字符串字段判断是否包含字符串的3中方法 方法一: select * from user where email like "%b@email.com%";// 这个理解起 ...

  3. [WPF][ListBox]鼠标拖拽多选,(Shift Key、Ctrl Key多选有效)(转)

    <ListBox Name="listBox" SelectionMode="Extended"> <ListBox.Resources> ...

  4. 使用CSS设置Chrome打印背景色

    以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...

  5. HDU5124 lines

    离散化 + 树状数组. 这些东西自己都是刚接触不久的,所以需要多写点题练练手. 题目描述: 一维坐标中有N条线段,其中有一个点上面覆盖的线段数是最多的,求该点上面的线段数目. 这道题和HDU1556特 ...

  6. innerHTML和outerHTML的区别

    一.区别:1)innerHTML: 从对象的起始位置到终止位置的全部内容,不包括Html标签.2)outerHTML: 除了包含innerHTML的全部内容外, 还包含对象标签本身. 二.例子: &l ...

  7. AOJ 558 Cheese(bfs)

    题意:网格图,老鼠吃奶酪,吃完奶酪体力值会增加,只能吃硬度不大于体力值的,问最小步数. 思路:按硬度从小到大的吃起,依次求最短路. 我用曼哈顿距离估价的A*,和普通bfs的time没区别啊,还把优先级 ...

  8. Using an Image for the Layer’s Content

    Using an Image for the Layer’s Content Because a layer is just a container for managing a bitmap ima ...

  9. mkfs.xfs 命令找不到的解决方法

    对硬盘进行格式化: # mkfs.xfs /dev/sdb1 系统显示: mkfs.xfs error: command not found. 可能是系统不完全安装 运行 which mkfs  查看 ...

  10. Nginx学习记录(二)

    1. 什么是反向代理 正向代理 反向代理: 反向代理服务器决定哪台服务器提供服务. 返回代理服务器不提供服务器.也是请求的转发. 反向代理(Reverse Proxy)方式是指以代理服务器来接受Int ...