题目:给你一个数n,判断n是不是某个数的幂,,这个数题目里会给出,leetCode上有三道题,分别是2的幂,3的幂,4的幂
思路:方法都一样,while循环而已
//2的幂 public class Solution { public boolean isPowerOfFour(int num) { if(num>1){ while(num%2==0) num/=2; } return num==1; } } //3的幂 public class Solution { public boolean isPowerOfFour(int num) { if(num>1){ while(num%3==0) num/=3; } return num==1; } } //4的幂 public class Solution { public boolean isPowerOfFour(int num) { if(num>1){ while(num%4==0) num/=4; } return num==1; } }
时间: 2024-10-12 21:58:01