326.power-of-three.go 200 B

123456
  1. func isPowerOfThree(n int) bool {
  2. if n <= 0 { // The power is non-negative.
  3. return false
  4. }
  5. return 1162261467 % n == 0 // A magic number! (Actually, is the maximum of pow(3, x) in [1, 1<<32-1])
  6. }