Skip to content
the poor coder

Leetcode - Power of Three Solution

Poor Man
Published
Updated
Reading time
1 min read

Given an integer n, return true if it is a power of three. Otherwise, return false.

An integer n is a power of three, if there exists an integer x such that n == 3x.

Example 1:

Text
Input: n = 27
Output: true

Example 2:

Text
Input: n = 0
Output: false

Example 3:

Text
Input: n = 9
Output: true

Example 4:

Text
Input: n = 45
Output: false

Constraints:

  • -231 <= n <= 231 - 1

Follow up: Could you solve it without loops/recursion?

Solution in python#

Python
class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n <= 0:
            return False
        return (math.log10(n) / math.log10(3))%1 == 0

Keep going

2021

Leetcode - Maximum 69 Number Solution

Given a positive integer num consisting only of digits 6 and 9. Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6). Example 1: Example 2: Example 3: Constraints: 1 <= num…

leetcodeMath1 min read
ESC