Given an integer, write a function to determine if it is a power of two.

Solution

public class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n == 0) return false;
        int t = (int)(Math.log(n) / Math.log(2));
        return n == Math.pow(2,t);
    }
}

results matching ""

    No results matching ""