69 Sqrt(x) – Medium¶
Problem:¶
Implement int sqrt(int x).
Compute and return the square root of x.
Thoughts:¶
The most straight forward way it so iterate over all possible numbers, namely 1 to x/2+1.
But we could find it faster by using binary search. improving running time from O(x) to O(logx).
Plus, the condition to determine result is also critical, because we cannot say m*m == x is the checking condition.