Title: Power of Three Source: leetcode.com
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
Java solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
/*https://leetcode.com/problems/power-of-three/*/ public class PowerOfThree { public boolean isPowerOfThree(int n) { if(n==0) return false; while(n!=1) { //System.out.println(n); int remainder = n%3; if(remainder!=0) return false; n = n/3; } return true; } public static void main(String args[]) { PowerOfThree p = new PowerOfThree(); System.out.println(p.isPowerOfThree(6)); } } |