Given an integer, write a function to determine if it is a power of three.
Example 1:
Input: 27Output: true
Example 2:
Input: 0Output: false
Example 3:
Input: 9Output: true
Example 4:
Input: 45Output: false
Follow up:
Could you do it without using any loop / recursion?public static Listlist = null; static{ list = new ArrayList<>(); for(int i= 0;i (long)Integer.MAX_VALUE){ break; } list.add(value.longValue()); } } //发现先把数据取出来 比对一下 妥妥的 但是用了loop 哎 public static boolean isPowerOfThree(int n) { return list.contains((long)n); } //看看大神的表演 原来这样就可以啦 public static boolean isPowerOfThree2(int n) { // 1162261467 is 3^19, 3^20 is bigger than int return ( n>0 && 1162261467%n==0); } //好吧 都用上公式啦 public static boolean isPowerOfThree3(int n) { return (Math.log10(n) / Math.log10(3)) % 1 == 0; } public static void main(String[] args) { System.out.println(isPowerOfThree3(9)); System.out.println(isPowerOfThree3(27)); System.out.println(isPowerOfThree3(0)); System.out.println(isPowerOfThree3(45)); //这里会是什么// Integer a = 6;// Long b = 6L;// System.out.println(a.equals(b)); }
git:https://github.com/woshiyexinjie/leetcode-xin