69. Sqrt(x)
2016-03-22 10:26
246 查看
Implement
Compute and return the square root of x.
Subscribe to see which companies asked this question
int sqrt(int x).
Compute and return the square root of x.
Subscribe to see which companies asked this question
public class Solution { public int mySqrt(int x) {//中间步骤要用long类型,不然会溢出 if(x<=0)return x; long a = (long)x; int i = 1; while((long)i*i<a){ i=i<<1; } int tem = i; int ret = 0; for(;tem>0;tem=tem>>1){ if((long)(ret+tem)*(ret+tem)<=x)ret+=tem; } return ret; } }
相关文章推荐
- iOS开发之真机获取NSLog的日志文件
- PHP通用分页组件
- linux mplayer 播放yuv格式 (转载)
- jQuery实现当拉动滚动条到底部加载数据
- Java 四种线程池
- 如何用tomcat发布自己的Java项目
- Android WebView的Js对象注入漏洞解决方案
- 在应用角度Docker容器技术带来的改变
- swift学习资源
- 简单的毫秒级C语言耗时打印宏
- nodejs aes加解密128算法,base64编码
- Linux下配置jdk1.7
- 手机GUI自动化测试介绍 | 有道技术沙龙博客
- ACdream OJ 1153 (k-GCD)
- 让你的WizFi250适应各种气候
- js如何给当前日期+1?
- Hibernate exercise 54
- HTML 表单(form) 使用详解
- [置顶] iOS微信支付的步骤以及问题描述
- 部分网站中加载外部js 禁止的疑问