Precomputing C# solution

  • -2
    public class Solution {
        public bool IsPowerOfThree(int n) {
            HashSet<double> computedPowersOfThree = new HashSet<double>();
            for (int i = 0; i < 64; ++i)
                computedPowersOfThree.Add(Math.Pow(3, i));
            return computedPowersOfThree.Contains((double) n);

    I used a HashSet for constant time insertion and lookup.

Log in to reply

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