leetcode Question: Power of Two

Power of Two

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

Analysis:

An straightforward way is to keep doing "divide by 2" for the number and check if current number "mod 2" is zero. However this is pretty slow (it still can pass the OJ).

More efficient way is to use bit manipulation.

If one number is a power of 2, then its binary must starts with 1 and all the lower bits are 0. e.g.,
2 10
4 100
8 1000
16 10000
...

Also, we have to find a "mask" to check this format, what "mask" should we use?
Let's see some examples:
1 01
3 011
7 0111
15 01111
...

Well, it is not hard to see that,  if one number "n" is a power of 2, then "n-1" must have all 1s in its binary format. Therefore, "n & n-1" must be 0.  


Code(C++):

class Solution {
public:
    bool isPowerOfTwo(int n) {
        return ( (n>0) && (n & (n-1))==0 ) || (n==1);
    }
};

Code(Python):

aclass Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        return n>0 and  n & (n-1)==0 or n==1


109 comments:

  1. Or can we take log base 2 of n, if ans is integer, that means it's power of 2.

    ReplyDelete
  2. Or can we take log base 2 of n, if ans is integer, that means it's power of 2.

    ReplyDelete
  3. I am really happy to say it’s an interesting post to read . I learned w information from your article , you are doing a great job . Thanks for a information

    123.hp.com/setup 4625

    ReplyDelete
  4. Excellent post, thanks for this resource!

    123 hp setup

    ReplyDelete
  5. Really its very useful information that you have shared and thanks for sharing the information with us.
    Hp Officejet 5746 Troubleshooting

    ReplyDelete
  6. Excellent solution for setup hp printers. Thanks for sharing. 123.hp.com/setup 4650

    ReplyDelete
  7. Immense articles 123.hp.com/setup Thanks for sharing useful information.

    ReplyDelete
  8. Cheerful Training 123.hp.com , Cheering Unit 123.hp.com , Cheery Vignette 123.hp.com , Cherished Accepted 123.hp.com , Chic Acknowledgement 123.hp.com

    ReplyDelete
  9. Disciplined Judgement 123.hp.com , Distinct Key 123.hp.com , Diverting Knowledge 123.hp.com , Doable Leading 123.hp.com

    ReplyDelete
  10. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.


    HP Printer Setup

    ReplyDelete
  11. Hi, thank you very much for help. I am going to test that in the near future. Cheers


    123.hp.com/oj4650 for mac

    ReplyDelete
  12. https://yucoding.blogspot.com/2015/12/leetcode-question-power-of-two.html?showComment=1551172487473#c9092341443477290844

    ReplyDelete
  13. Visit to Printer Troubleshooting
    Solved all your hp printer issues,visit our site
    #Printer #Wireless #Mac #Software #Driver #Download

    ReplyDelete
  14. Visit to Printer Driver Install
    Solved all your hp printer issues,visit our site
    #Printer #Wireless #Mac #Software #Driver #Download

    ReplyDelete
  15. To use the Xfinity.com/authorize Stream Beta channel, you may need a Comcast account, a working Xfinity.com/authorize TV, Instant TV, or Xfinity.com/authorize on Campus registration, one essential set-top box, and Internet administration.

    ReplyDelete
  16. Good day. I was impressed with your article. Keep it up . You can also visit my site if you have time. Thank you and Bless you always.
    hp officejet 3833 trouble shooting

    ReplyDelete
  17. hey there are you confused on installing setup for hp printers.
    we are providing installation guide and for hp printers visit 123.hp.com and 123.hp.com/setup 123.hp.com/setup 5660

    123.hp.com/setup 7640

    123.hp.com/setup 3830

    123.hp.com/setup 5255

    ReplyDelete
  18. Rent a car Islamabad is one of the most trustworthy and renowned name of the VIP car rental market place in Pakistan.
    We offer premium car rental service at affordable charges to keep your traveling need absolutely budget friendly.
    rent a car islamabad

    ReplyDelete

  19. latest form get all latest government jobs, bank jobs update

    ReplyDelete
  20. Really its very useful information that you have shared and thanks for sharing the information with us.123.hp.com/ojpro6968

    ReplyDelete
  21. Your article was really impressive.Do keep post like this . You can also visit my site if you have time.Kindly visit our site for123.hp.com/ojpro6978

    ReplyDelete
  22. Geek Squad Tech Support helps those customers who face technical issues in their own gadgets anytime and unable to sort out at this place Geek Squad Customer Service Team Aid your issues by manually or through the software on Remote. Call and Get Geek Squad Appointment on (+1)855-554-9777 for technical issues in Gadgets.

    Geek Squad Appointment | Geek Squad Customer Service Number | Geek Squad Online Help Number | Geek Squad Scheduling | Geek Squad Tech Support | Geek Squad Webroot Support | Best Buy Geek Squad | Geek Squad Support | Geek Squad Norton Support

    ReplyDelete
  23. thanks for your informative post.123.hp.com/setup OJ.here my new blog .

    ReplyDelete



  24. Thanks for sharing such a good content with your blogs. I really enjoying your blog while reading.
    printer driver support number
    brotehr printer driver support

    ReplyDelete
  25. Your blog always offer some really informative information. Thank you for sharing it with us. If you required Roku Com Link Code than contact our team.
    Roku Com Link
    Enter Link Code
    Link Activation Roku

    ReplyDelete
  26. How to fix an Offline HP Printer
    Whenever you encounter an offline printer error, it is advisable to check if your PC or laptop has the latest version of the Windows update installed. This includes an update that enhances the discoverability of printers. To troubleshoot your offline printer, try the following steps:




    https://www.realprinterfix247.us/fix-hp-printer-is-offline-issue-windows-10/

    ReplyDelete
  27. eally awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.

    HP Officejet 6950 Printer

    ReplyDelete
  28. Excellent information with unique content and it is very useful to know about the information based on blogs.
    Best Guide for 123 HP Setup & Install

    ReplyDelete

  29. This post will be very useful to us....i like your blog and helpful to me....nice thoughts for your great work....

    HP Envy 7645 Wireless Setup & Install

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete
  31. Great information provided by you... if you facing this same issues the dial visit our blog:-
    hp envy 5055 wireless setup

    ReplyDelete
  32. Lexmark Printer Support
    123 hp setup

    123.hp.com/setup
    <a href="https://www.youtube.com/watch?v=daFrD-BfQFs&feature=youtu.be>Best Tips and Tricks you should know to grow youe online business 2020</a>

    ReplyDelete
  33. Leader in homesourced Printer customer Help solutions with a secure, proprietary omnichannel platform & tools and a global network of home-based agents.

    ReplyDelete
  34. You are providing Right Informartion here. I Got help with your blog . It realy simple and helpful
    123.hp.com/setup
    Best Web Development Company US

    ReplyDelete

  35. Setting up the HP printer gets more comfortable and straightforward via 123.hp.com. It is an official online platform from where the user can execute the setup procedure quickly. Through this official website, the user can perform the setup procedure for the following hp printer, i.e., "OfficeJet," "DeskJet," "ENVY," "LaserJet."

    The user can easily install the hp smart software and drivers on their mobile or computer device through which they want to print. To accomplish the setup procedure in a stress-free manner, visit the official website, i.e., 123.hp.com.

    Along with the setup procedure, the users can also download drivers, contact the HP printer customer service team, and perform many other tasks. So, once the user visits the official website, i.e., 123.hp.com, they will be asked to enter the product name. Moreover, after entering the model number, click on the next option to proceed with the setup procedure.

    ReplyDelete
  36. We are a completely preferred specialized guide, which is assisting all printer users online 24 hours for any type of technical problem. If you need to setup HP printer using 123 HP Deskjet 3630 Printer Setup, you can get the step by step technical instruction for setting up an HP printer in the direct ways. In the initial step, you want to open your web page in the choose browser.

    ReplyDelete

  37. Nice information. Thanks for sharing content and such nice information for me. I hope you will share some more content about. Please keep sharing!
    HP ENVY 7640 Installation Download

    ReplyDelete
  38. Nice Information , Thanks For The Great Content

    Get started to Install, Setup, Connect, Print from your 123 hp setup printers. Easy to Download driver & Printer software from HP Envy,HP Officejet,HP Officejet Pro,HP Deskjet Printer Setup Driver Installation

    For More Support

    123.hp.com/Setup
    123hp com/oj8049
    123.hp.com/oj3830
    123.hp.com/oj4650
    123.hp.com/Setup ojpro
    123.hp.com/ojpro6968

    ReplyDelete
  39. Great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
    123.hp.com/oj6950 Setup

    ReplyDelete
  40. The complete guide for solving Printer Not Printing Black issues is introduced on that page. So, open it on your browser and go through the provided instructions one-by-one. Surely, your printer will start printing correctly in black color.123.hp.com/setup envy 5020

    ReplyDelete
  41. Amazon Prime Video may be seen on a variety of devices, including computers, mobile phones, and smart TVs. Enter the Amazon Code, which is 5-6 digits long.

    amazon.com/mytv
    amazon/mytv
    amazon.com/mytv
    amazon.com/mytv

    ReplyDelete
  42. In the event that you are a client of HP printer and searching for a source that can give you definitive advances and documents for the setup of HP printer, mind me, you should visit 123.hp.com/ojpro8710.

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. Do you meet your hp printer in printer offline because of any interrupted in wireless connection, disconnection printer receiver problem, printer not connected with the devices, so it may be very difficult or easy to fix based on their type of problem so feel free, we are here for fixing your hp printer offline problem by well-experienced printer technician by through online session of our website hp printer in offline

    ReplyDelete
  45. Great experience, Thanks for the valuable and useful information. Very useful advice in this particular article. For any queries related to HP ENVY 5055 PRINTER SETUP HP ENVY 5000 PRINTER SETUP Check in to our site..!!

    ReplyDelete
  46. Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome! For any support related to How To Do Hp Envy 7855 Printer Setup?, HP Envy 7830 Setupkindly follow our site.

    ReplyDelete
  47. Useful information. Thanks for sharing.
    Need Epson printer driver assistance? Feel free to reach our website!
    Epson printer driver | Epson connect printer setup utility 

    ReplyDelete
  48. Willkommen in der e-Apotheke für intelligente Medikamente. Generisches Modafinil ist von jedem erhältlich.
    sites.google

    ReplyDelete