首页 > 代码库 > LeetCode----326. Power of Three(Java)

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                         else22                             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         else41             return false;42             43             44     }45     public static void main(String[] args) {46         // TODO Auto-generated method stub47         System.out.println(isPowerOfThree(-3));48     }49 50 }

 

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