mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Add solution to "69. Sqrt(x)" in C++
This commit is contained in:
16
Easy/sqrtx/solution.cpp
Normal file
16
Easy/sqrtx/solution.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int mySqrt(int x) {
|
||||||
|
if (x == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int times = 20;
|
||||||
|
double xi = x;
|
||||||
|
for (int i = 0; i < times; ++i) {
|
||||||
|
xi = (xi + x / xi) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)xi;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user