Solution by evgeniykuz

  • 0

    Approach #1 The Right Way


    For parameter x set type Double, then use mathematical function sqrt() and convert again to type Int


    • Convert parameter x to Double, because mathematical function sqrt() requires number which type is Double
    • Convert result of mathematical function sqrt() to Int, because our function return number with type Int


    class Solution {
        func mySqrt(_ x: Int) -> Int {
            return Int(sqrt(Double(x)))

    Complexity Analysis

    It is very simple solution, so in general, mathematic function sqrt() does the main work. We must only change type of parameter x to Double, because we need it for function sqrt(), and then again covnvert result of this function to type Int, because type of our return parameter is Int.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.