Simple c++ solution with comments


  • 0
    R

    class Solution {
    public:
    bool isPowerOfTwo(int n) {

        if(n <= 0) return false;            // Base case return false for any negative or 0 input
        while(n%2 == 0){                   // Divide by 2 as long as reminder is 0
            n = n/2;
        }
        if(n == 1) return true;            // The result must be 1 if the original input was Power of 2
        else return false;
    }
    

    };


Log in to reply
 

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