[leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
class Solution {
public:
int sqrt(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<=) return ;
if(x==) return ;
int small=;
int large=x;
int temp=x/;
while(small<large){
int a = x/temp;
int b = x/(temp+);
if (a==temp) return a;
if (b==temp+) return b;
if(temp<a && temp+>b)
return temp;
else if(temp<a && temp+<b){
small=temp+;
temp = (small+large)/;
}else {
large = temp;
temp = (small+large)/;
}
}
return -;
}
};
我的答案
思路:需要在O(log n)时间内实现,并且注意用除法,防止两个int型的数相乘超过int的范围。
[leetcode.com]算法题目 - Sqrt(x)的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...
随机推荐
- Activiti5 添加/查询审批批注(审批意见)
Activiti5 添加/查询审批批注 Activiti 工作流开发,23张表中,act_hi_commit 中,用于保存流程审核的批注信息: 调用: taskServer.addComment ...
- 使用 CXF 做 webservice 简单例子(转载)
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...
- 2017/2/16:自己ajax+json习惯性写法 代码拼接的写法 +json用post提交乱码的原因
1.先导入jquery的包 2.ajax的写法跟注意点 返回一个list的写法 代码拼接写法: html层: 2.script处 4:在你前面传递参数的时候没有遇到乱码问题的情况下,你使用json并且 ...
- Find the location of libmysqlclient.so.X file in Linux environments
I'm putting together a script that has a requirement of knowing libmysqlclient.so.[15|16|18] .so fil ...
- 单片机一种简便的printf调试方案。
此处引用csdn博客.链接如下. http://blog.csdn.net/cp1300/article/details/7773239 http://blog.csdn.net/aobai219/a ...
- ssh定义、操作
Secure Shell(縮寫为SSH)SSH為一项建立在应用层和传输层基础上的安全协议,为计算机上的Shell(壳层)提供安全的传输和使用环境. 传统的网络服务程序,如rsh.FTP.POP和Tel ...
- bootstrap之css样式
一 bootstrap的介绍 Bootstrap是将html,css和js的代码打包好了,只管我们拿来调用.是基于jquery开发的. 使用BootCDN提供的免费CDN加速服务,同时支持http和h ...
- 基础知识之nginx重写规则
nginx重写规则 nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f ...
- 2019.01.21 NOIP训练 ak树(点分治)
传送门 题意简述:给一棵带权树,问在上面随机选两个点距离是4的倍数的概率. 思路: 由于总方案数为定值n2n^2n2,所以只用求总方案数. 这个跟聪聪可可差不多,可以用类似树形dpdpdp的方法边点分 ...
- 2018.12.30 洛谷P4238 【模板】多项式求逆
传送门 多项式求逆模板题. 简单讲讲? 多项式求逆 定义: 对于一个多项式A(x)A(x)A(x),如果存在一个多项式B(x)B(x)B(x),满足B(x)B(x)B(x)的次数小于等于A(x)A(x ...