-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0326-Power-of-three.cs
More file actions
62 lines (52 loc) · 1.38 KB
/
0326-Power-of-three.cs
File metadata and controls
62 lines (52 loc) · 1.38 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0326.Power_of_three
{
public class _0326_Power_of_three
{
/// <summary>
/// Soluton 1
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public bool IsPowerOfThree(int n)
{
if (n < 1) return false;
while (n % 3 == 0) n /= 3;
if (n == 1) return true;
return false;
}
/// <summary>
/// Solution 2
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
//public bool IsPowerOfThree(int n)
//{
// if (n < 1) return false;
// while (n > 1)
// {
// if (n % 3 != 0)
// return false;
// n /= 3;
// }
// return true;
//}
/// <summary>
/// Solution 3
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
//public bool IsPowerOfThree(int n)
//{
// if (n == 1 || n == 3 || n == 9) return true;
// if (n < 9) return false;
// while ((n > 9) && (n % 9 == 0))
// n /= 9;
// while ((n > 3) && (n % 3 == 0))
// n /= 3;
// return n % 3 == 0;
//}
}
}