LeetCode----326. Power of Three(Java)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode----326. Power of Three(Java)相关的知识,希望对你有一定的参考价值。

 1 package isPowerOfThree326;
 2 /*
 3  Given an integer, write a function to determine if it is a power of three.
 4  */
 5 public class Solution {
 6     /*
 7     //题目理解错误,理解成3次开方
 8     public static boolean isPowerOfThree(int n) {
 9         if (n==1)
10             return true;
11         else{
12             for(int i=2;i<=Math.sqrt(n);i++){
13                 if(n%i==0){
14                     n=n/i;
15                     if(n%i==0){
16                         n=n/i;
17                         if (n%i==0){
18                             n=n/i;
19                             return isPowerOfThree(n);
20                         }    
21                         else
22                             return false;
23                     }
24                     return false;
25                 }
26             }
27             return false;
28         }
29     }
30     */
31     public static boolean isPowerOfThree(int n){
32         if (n==1)
33             return true;
34         else if (n==0)
35             return false;
36         else if (n%3==0){
37             n=n/3;
38             return isPowerOfThree(n);
39         }
40         else
41             return false;
42             
43             
44     }
45     public static void main(String[] args) {
46         // TODO Auto-generated method stub
47         System.out.println(isPowerOfThree(-3));
48     }
49 
50 }

 

以上是关于LeetCode----326. Power of Three(Java)的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode----326. Power of Three(Java)

LeetCode - 326. Power of Three

LeetCode-326. Power of Three

39. leetcode 326. Power of Three

LeetCode 326. 3的幂 Power of Three

LeetCode 326. 3的幂 Power of Three