One day a leetcode #342. Power of Four__github

Source: Internet
Author: User
One leetcode a day .

This series of articles has been uploaded to my Github, address: Zeecoder ' s Github
Welcome to my Sina Weibo Weibo, my Sina Weibo
Welcome reprint, Reprint please indicate the source (i) topic

Given an integer (Signed/bits), write a function to check whether it are a power of 4.

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

Follow up:could You solve it without loops/recursion? (ii) problem solving

The main idea: to determine whether a number is not 4 of the N-second square number.
Problem-solving thinking: can refer to: "One day a Leetcode" #326. Power of three and "one day Leetcode" #231. Power of two
First, the easiest way to do this is by looping:

Class Solution {public
:
    bool ispoweroffour (int num) {
        int n = num;
        while (n>0&&n%4==0)
        {
            n/=4
        }
        return n==1;
    }
;

There are questions in the question that can not do without circulation and return to do, and judge 3 of the N-second square number, 4 of the number of n times in the plastic number of only 16, can quickly enumerate, also is a good way.
In addition, we can see that the N-second square number of 4 and the N-second square number of 2 are able to get the n-th number of integers of 4 in 2 of the n-th square number.

Class Solution {public
:
    //Three conditions: greater than 0, is 2 of the N-second square number, after the root of the integer
    bool ispoweroffour (int num) {return
        (num>0) &&! (num& (num-1)) && (sqrt (num) *sqrt (num) ==num);
    }
;

It was ingenious to see such an answer in Leetcode's discussion area.
First determine whether NUM is 2 N-second square number, in judging him and the 0xaaaaaaaa phase is 0.
A characteristic of the N-second square number of 4 is that at bit bit, the 0,2,4,6,8,10 is 1,
So, first judge is not 3 of the N-second square number, it is guaranteed to have and only one on the number of 1, and then eliminate the odd digits of 1 of the number, the rest is 4 of the N-second square number.

Class Solution {public
:
    bool ispoweroffour (int num) {
        //three conditions: is greater than 0, is 2 of the N-second square number, and has only even digits on 1 return
        (num> 0) &&! (num& (num-1)) &&! (NUM&0XAAAAAAAA);
    }
;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.