Question in 231. Power of Two


  • 0
    O

    Re: Error in Power of Two question
    Question: why wrong when I cut down the final abs???
    //right version
    class Solution {
    public:
    bool isPowerOfTwo(int n) {
    double x = log(n) / log(2);
    return (abs((x - (int)(x+0.5)))< 1e-10); //why wrong when I cut down the abs???
    }
    };
    //wrong version:
    class Solution {
    public:
    bool isPowerOfTwo(int n) {
    double x = log(n) / log(2);
    return ((x - (int)(x+0.5))< 1e-10); //why wrong when I cut down the abs???
    }
    };


  • 0
    S

    Hi, Could you please explain the solution. It will be very useful for me.


  • 0
    O

    @sugan
    please say something about which part you do not understand, and it will convient me. And my code is based on knowledge of log.


  • 0
    S

    @Offer2017
    Hi,
    actually I dont understand how you are using log to get to the solution. And why do u do log(n)/log(2).
    And what does the return statement mean.


Log in to reply
 

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