Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Power of Three LeetCode Solution

Problem – Power of Three

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:

Input: n = 27
Output: true

Example 2:

Input: n = 0
Output: false

Example 3:

Input: n = 9
Output: true

Constraints:

  • -231 <= n <= 231 - 1

Power of Three LeetCode Solution in Java

public class Solution {
public boolean isPowerOfThree(int n) {
    // 1162261467 is 3^19,  3^20 is bigger than int  
    return ( n>0 &&  1162261467%n==0);
}

Power of Three LeetCode Solution in C++

class Solution {
public:
    bool isPowerOfThree(int n) {
        return fmod(log10(n)/log10(3), 1)==0;
    }
};

Power of Three LeetCode Solution in Python

class Solution(object):
    def isPowerOfThree(self, n):
        return n > 0 and 1162261467 % n == 0
Power of Three LeetCode Solution Review:

In our experience, we suggest you solve this Power of Three LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Power of Three LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Power of Three LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *