We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2281403 commit 0b0d20dCopy full SHA for 0b0d20d
PowerOfThree.java
@@ -0,0 +1,35 @@
1
+/*
2
+Given an integer n, return true if it is a power of three. Otherwise, return false.
3
+An integer n is a power of three, if there exists an integer x such that n == 3x.
4
+
5
+Example 1:
6
+Input: n = 27
7
+Output: true
8
+Explanation: 27 = 33
9
+ */
10
+public class PowerOfThree {
11
+ public static void main(String[] args) {
12
+ int n=27;
13
+ boolean ans=isPowerOfThree(n);
14
+ System.out.println(ans);
15
+ }
16
+ public static boolean isPowerOfThree(int n) {
17
+ if(n==0){
18
+ return false;
19
20
+ int num=n;
21
+ boolean ans=true;
22
+ while (num>0){
23
+ if(num==1){
24
+ return true;
25
26
+ if (num%3==0){
27
+ num=num/3;
28
29
+ else {
30
31
32
33
34
35
+}
0 commit comments