Given an integer, write a function to determine if it is a power of.
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
Given an integer, write a function to determine whether it is a power of 2.
Example 1:
Input: 1 output: true explanation: 20 = 1
Example 2:
Input: 16 Output: true explanation: 24 = 16
Example 3:
Input: 218 Output: false
24ms
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3 ifN <1{return false}4 varNum:int =N5 while(num! =1)6 {7 ifnum%2==1{return false}8 Else{num/=2}9 }Ten return true One } A}
24ms
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3Guard n >0 Else{return false }4 ifn = =1{return true }5 varnum =26 whileNum <N {7Num *=28 }9 returnnum = =NTen } One}
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3Guard n >0 Else{return false }4 ifn = =1{return true }5 varnum =26 whileNum <N {7Num *=28 }9 returnnum = =NTen } One}
20ms
1 classSolution {2Func Ispoweroftwo (_ N:int)Bool {3 ifn = =1 {4 return true5 }6 7 varX:int =28 while(x <=N)9 {Ten ifx = =N { One return true A } - -X *=2 the } - return false - } -}
20ms
1 class Solution {2 func ispoweroftwo (_ N:int), Bool {3 // Power of 1 '4 return15 } means 6 }
231. Power of 2 | Power of