题目:

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. Tomcat8

    一.Apache Tomcat 8介绍 Tomcat 8.0.0-RC3 (alpha) Released                                                ...

  2. Eucalyptus常用查询命令

    前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus)  ...

  3. WPF创建SignalR服务端(转)

    在网上看到了一个帖子,比较详细,博主写的很好. 地址:http://blog.csdn.net/lordwish/article/details/51786200

  4. Hadoop之—— WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...

    [hadoop@hadoop000 hadoop]$ ldd --version ldd (GNU libc) 2.12 Copyright (C) Free Software Foundation, ...

  5. python 之开发工具 sublimetext 3

    一.前言 由于个人工作内容太过于繁杂,记忆力又不好,为日后使用的方便,故简单的记录了本篇关于sublimetext 3的初始化安装和部分插件内容的记录.目前最新的版本也是3.0以上版本了,故我这里使用 ...

  6. OpenSSL命令---s_client

    http://blog.csdn.net/as3luyuan123/article/details/16812071 用途: s_client为一个SSL/TLS客户端程序,与s_server对应,它 ...

  7. log4j 配置文件 (XML/.properties)

    xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configurat ...

  8. 【洛谷4149】[IOI2011] Race(点分治)

    点此看题面 大致题意: 给你一棵树,问长度为\(K\)的路径至少由几条边构成. 点分治 这题应该比较显然是点分治. 主要思路 与常见的点分治套路一样,由于\(K≤1000000\),因此我们可以考虑开 ...

  9. java 类和成员的修饰符

  10. 网络编程——网络模型和InetAddress类

    第1章 网络通信协议 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样.在计算机网络中,这些连接和 ...