1 line c++ power solution


  • 0
    A

    I interpreted the problem prompt as not being able to use the builtin Math library.
    I did however decide to post this solution, which involves knowledge of the c++ library and slight math.

    Knowing that num^(1/2) == sqrt(num), and that the c++ power function will return a double if the num^(1/2) is not a perfect square and an integer if it is, I came up with:

        bool isPerfectSquare(int num) {
            return int(pow(num,0.5)) != pow(num, 0.5)? false: true;
        }
    

    Calling the integer constructor upon the power function will truncate or discard any additional floating point values, which means that if the number is not a perfect square, these values will not match.


  • 0
    J
    This post is deleted!

  • 0
    A

    Which is why I put this,
    "I interpreted the problem prompt as not being able to use the built-in Math library.
    I did however decide to post this solution."


Log in to reply
 

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