-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0326.go
49 lines (43 loc) · 1.03 KB
/
0326.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Source: https://leetcode.com/problems/power-of-three
// Title: Power of Three
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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:
//
// -2^31 <= n <= 2^31-1
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func isPowerOfThree(n int) bool {
if n < 1 {
return false
}
for n%3 == 0 {
n /= 3
}
return n == 1
}
// 1162261467 is the max power of three in 32-bit integer
func isPowerOfThree2(n int) bool {
return n > 0 && 1162261467%n == 0
}