mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 10:44:06 +00:00
32 lines
488 B
C++
32 lines
488 B
C++
#include "soln.hpp"
|
|
#include <cmath>
|
|
|
|
int Solution::mySqrt(int x)
|
|
{
|
|
// base case
|
|
if (x == 0 || x == 1)
|
|
{
|
|
return x;
|
|
}
|
|
// effectively only till ~sqrt(x)
|
|
for (int i = 2; i <= x; i++)
|
|
{
|
|
long i_square = long(i) * i;
|
|
// x is a perfect square
|
|
if (i_square == x)
|
|
{
|
|
return int(i);
|
|
}
|
|
// x is not a perfect square
|
|
else if (i_square > x)
|
|
{
|
|
return int(i - 1);
|
|
}
|
|
}
|
|
return -1; // compiler fuss
|
|
}
|
|
|
|
bool Solution::test(int x, int answer)
|
|
{
|
|
return answer == mySqrt(x);
|
|
} |