Leetcode69. Sqrt(x)
来源:互联网 发布:知到生态文明答案 编辑:程序博客网 时间:2024/05/01 22:52
题目描述:
Implement int sqrt(int x).Compute and return the square root of x.
题目要求:
要求我们求取x的平方根
题目分析:
本题与之前的Arranging Coins那道题很相似,都是可以使用一般的迭代方法O(n)的时间复杂度,和二分法O(logn)的时间复杂度http://blog.csdn.net/qq_27896185/article/details/53406632。
具体实现:
public class Solution { public int mySqrt(int x) { long start = 1; long end = x; //找到第一个比x小的位置,返回。 while (start + 1 < end) { long mid = start + (end - start)/2; if (mid * mid <= x) { start = mid; } else { end = mid; } } if (end * end <= x) { return (int)end; } return (int)start; } }
0 0
- LeetCode69 Sqrt(x)**
- [Java]Leetcode69 Sqrt(x)
- leetcode69. Sqrt(x)
- Leetcode69. Sqrt(x)
- leetcode69. Sqrt(x)
- leetcode69. Sqrt(x)
- leetcode69: Sqrt(x)
- LeetCode69. Sqrt(x)
- LeetCode69 Sqrt(x)
- leetcode69---Sqrt(x)
- LeetCode69——Sqrt(x)
- leetcode69---Sqrt(x)(求x的平方根)
- Sqrt(x)
- Sqrt(x)
- Sqrt(x)
- Sqrt(x)
- Sqrt(x)
- sqrt(x)
- Hadoop HDFS Shell命令
- 七款嵌入式Linux操作系统简介
- 在win7的idea访问centos部署的hadoop2.7.1问题集锦
- 【转载】Linus下的/etc/hosts文件详解
- 【转载】MapReduce模型
- Leetcode69. Sqrt(x)
- 迭代开发
- java.lang.NullPointerException at org.apache.jsp.***_jsp._jspInit(***_jsp.java:22)
- 秒杀高并发厂家解决方案分享
- ELK搭建
- Docker与微服务培训【PPT截图】
- sftp工具类【附代码DEMO】
- XMLHTTPRequest记录
- CVS导出工具【附DEMO】