码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode之数组处理题java

时间:2016-04-26 15:57:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

342. Power of Four

Total Accepted: 7302 Total Submissions: 21876 Difficulty: Easy

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

public class Solution {
    public boolean isPowerOfFour(int num) {
        if(num==0)
            return false;
        while(num%4==0){
            num = num>>2;
        }
        if(num==1)
            return true;
        return false;
    }
}

326. Power of Three

Total Accepted: 37750 Total Submissions: 103178 Difficulty: Easy

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?

public class Solution {
    public boolean isPowerOfThree(int num) {
        if(num==0)
            return false;
        while(num%3==0){
            num = num/3;
        }
        if(num==1)
            return true;
        return false;
    }
}

231. Power of Two

Total Accepted: 70238 Total Submissions: 192439 Difficulty: Easy

Given an integer, write a function to determine if it is a power of two.

public class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n==0)
            return false;
        while(n%2==0){
            n = n>>1;
        }
        if(n==1)
            return true;
        return false;
    }
}

 

LeetCode之数组处理题java

标签:

原文地址:http://www.cnblogs.com/coffy/p/5435142.html

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