码迷,mamicode.com
首页 > 其他好文 > 详细

n的幂

时间:2017-05-22 20:04:02      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:font   span   sof   tco   leetcode   log   color   size   int   

题目:给你一个数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;
    }
}

 

n的幂

标签:font   span   sof   tco   leetcode   log   color   size   int   

原文地址:http://www.cnblogs.com/team42/p/6890962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!