342. Power of Four (Easy)

https://leetcode.com/problems/power-of-four/

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

Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?

Solutions

class Solution {

    public boolean isPowerOfFour(int num) {
        if (num <= 0) {
            return false;
        }

        int tmp = num;
        while (true) {
            if (tmp % 4 == 0) {
                tmp = tmp / 4;
                continue;
            }

            break;
        }

        if (tmp != 1) {
            return false;
        }

        return true;
    }
}

Incorrect Solutions

References

Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2020-07-03 00:26:46

results matching ""

    No results matching ""