31. Power of Two - Jake blog - Java coding practice log (2017)" /> 31. Power of Two - Jake blog - Java coding practice log (2017)" />

231. Power of Two


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

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

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"231. Power of Two"

147
0 0 147

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top