My solution in Swift

  • 0

    use math function log2 and floor.

    class Solution {
        func isPowerOfTwo(_ n: Int) -> Bool {
            if (n <= 0) {return false}
            var m = log2(Double(n))
            if floor(m) == m {
                return true
                return false

Log in to reply

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