leetcode 342. Power of Four

Original question can be found here.

My solution is as follows:

bool isPowerOfFour(int num)
{
        if(num < 1)
                return false;
        if(num == 1)
                return true;
        if(num%4 != 0)
                return false;
        else
                return isPowerOfFour(num/4);
}

Actually, there is a better, O(1) solution:

bool isPowerOfFour(int n) 
{
    return !(n&(n-1)) && (n&0x55555555);
    //check the 1-bit location;
}

For more imformation, click here.

27 thoughts on “leetcode 342. Power of Four”

  1. A simple type of the code:

    bool isPowerOfFour(int num)
    {
    if(num < 1 || num%4 != 0)
    return false;
    if(num == 1)
    return true;
    return isPowerOfFour(num/4);
    }

  2. Excellent post. I was checking constantly this weblog and I’m impressed!
    Very helpful information particularly the closing phase 🙂 I care for such information a lot.
    I used to be seeking this certain information for a very long time.
    Thank you and good luck.

  3. Simply to follow up on the up-date of this matter on your web-site and would really want to
    let you know simply how much I loved the time you took to produce this valuable post.

    Inside the post, you really spoke of how to
    seriously handle this concern with all convenience.

    It would be my pleasure to accumulate some more thoughts
    from your web page and come as much as offer other individuals what I have learned from you.

    Thank you for your usual wonderful effort.

  4. Pretty section of content. I simply stumbled upon your blog and
    in accession capital to assert that I acquire in fact loved account your weblog
    posts. Any way I will be subscribing in your augment and even I achievement
    you get entry to persistently rapidly.

  5. Great post. I used to be checking continuously
    this blog and I am impressed! Extremely useful information particularly the ultimate part 🙂 I take care of such information much.
    I was seeking this particular info for a long time.
    Thank you and best of luck.

  6. I enjoy you because of all of your hard work on this site.
    My mum delights in participating in research
    and it is easy to see why. All of us notice all about the compelling mode you make precious tips and hints through
    your website and even inspire response from others on this area plus our
    favorite child is certainly studying so much. Have fun with the remaining portion of the new year.
    Your doing a remarkable job.[X-N-E-W-L-I-N-S-P-I-N-X]I’m extremely inspired with your writing skills and also with the structure on your weblog.
    Is that this a paid subject or did you customize it yourself?
    Either way stay up the excellent quality writing, it’s uncommon to see a nice blog like this one nowadays.

    1. Thank you for your compliment, it makes me very happy.
      This website is powered by wordpress, Debian GNU/linux, Apache, MySQL, PHP etc.
      All are free softwares, thanks to the devolopers of these amazing softwares.
      I maintain it by myself.

  7. Definitely consider that that you stated. Your favorite
    reason appeared to be on the internet the easiest
    factor to consider of. I say to you, I definitely
    get annoyed while other folks consider worries that they plainly don’t realize about.
    You controlled to hit the nail upon the highest and also defined out the entire thing with no need side-effects , people
    could take a signal. Will probably be back to get more.
    Thanks

  8. Hi there! I understand this is sort of off-topic however
    I needed to ask. Does managing a well-established blog such as yours take a lot of work?

    I am completely new to running a blog however I do write in my journal every day.

    I’d like to start a blog so I will be able to share my experience and feelings online.
    Please let me know if you have any kind of recommendations
    or tips for new aspiring bloggers. Appreciate it!

  9. Thanks for one’s marvelous posting! I genuinely enjoyed reading it, you can be
    a great author.I will ensure that I bookmark your
    blog and will eventually come back sometime soon. I want to encourage that you continue your geat
    work, have a nice evening!

  10. This is really interesting, You are a very skilled blogger.
    I have joined your feed and look forward to seeking more of
    your wonderful post. Also, I’ve shared your web site in my social networks!

  11. I’m impressed, I must say. Seldom will i run into a blog that’s both educative and amusing, and
    without a doubt, you’ve hit the nail around the head.
    The issue is a thing that too few people are speaking
    intelligently about. Now i’m delighted that we found this inside my search for something regarding this.

Leave a Reply

Your email address will not be published. Required fields are marked *