leetcode 69. Sqrt(x)
2017-09-06 09:02
555 查看
Implement int sqrt(int x).
Compute and return the square root of x.
这道题考察的就是sqrt函数的实现,我是网上找到的一个教程做得,它适用的是牛顿迭代法直接迭代,我也不太懂,直接拿来用了。
代码如下:
下面是C++的做法,我就是按照cmath库函数做的
代码如下:
Compute and return the square root of x.
这道题考察的就是sqrt函数的实现,我是网上找到的一个教程做得,它适用的是牛顿迭代法直接迭代,我也不太懂,直接拿来用了。
代码如下:
public class Solution { public int mySqrt(int x) { //牛顿迭代法 long y=x; while(y*y>x) y=(y+x/y)/2; return (int)y; } }
下面是C++的做法,我就是按照cmath库函数做的
代码如下:
#include <iostream> #include <cmath> using namespace std; class Solution { public: int mySqrt(int x) { return (int)(sqrt(x)); } };
相关文章推荐
- Leetcode 69 Sqrt(x) 求x开根号的整数部分
- 【leetcode】69. Sqrt(x)(Python & C++)
- leetcode 69. Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- Leetcode-69.Sqrt(x)
- Leetcode-69. Sqrt(x)
- LeetCode||69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)解法及注释
- 【Leetcode】69.Sqrt(x)
- 【LeetCode】(69)Sqrt(x)(Medium)
- leetcode 69. Sqrt(x)
- [Leetcode]69.Sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode_69题——Sqrt(x)(二分查找,还有个溢出问题的研究)
- leetcode-69. Sqrt(x)
- 【LeetCode 69】Sqrt(x) (Python)
- LeetCode 69. Sqrt(x)
- [leetcode]: 69. Sqrt(x)
- LeetCode-69. Sqrt(x)
- 【Leetcode】69. Sqrt(x) 【溢出?如何防止?】