File tree 7 files changed +32
-0
lines changed
7 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfTwo (self , n : int ) -> bool :
3
+ return n > 0 and bin (n ).count ('1' ) == 1
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfTwo (self , n : int ) -> bool :
3
+ return n >= 1 and (n == 1 or n // 2 * 2 == n and self .isPowerOfTwo (n // 2 ))
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfThree (self , n : int ) -> bool :
3
+ if n <= 0 :
4
+ return False
5
+ while n != 1 :
6
+ prev = n
7
+ n //= 3
8
+ if n * 3 != prev :
9
+ return False
10
+
11
+ return True
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfThree (self , n : int ) -> bool :
3
+ return n >= 1 and (n == 1 or n // 3 * 3 == n and self .isPowerOfThree (n // 3 ))
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfThree (self , n : int ) -> bool :
3
+ return n > 0 and n == 3 ** int (log (n , 3 ) + 0.0001 )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfFour (self , n : int ) -> bool :
3
+ return n == 1 or \
4
+ n > 0 and (bin (n )[2 :][- 2 :].count ('1' ) == 0 ) \
5
+ and (bin (n )[- 3 :1 :- 2 ].count ('1' ) == 1 ) \
6
+ and (bin (n )[- 4 :1 :- 2 ].count ('1' ) == 0 )
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isPowerOfFour (self , n : int ) -> bool :
3
+ return n >= 1 and (n == 1 or n // 4 * 4 == n and self .isPowerOfFour (n // 4 ))
You can’t perform that action at this time.
0 commit comments