首页 > 代码库 > leetcode_Power of Two_easy
leetcode_Power of Two_easy
Given an integer, write a function to determine if it is a power of two.
题目意思:推断某个数是否是2的幂。
方法:直接进行bit运算,推断是否这个数二进制位里有且仅有一个1。
class Solution { public: bool isPowerOfTwo(int n) { int c=0; while(n!=0) { c+=1&n; if(c>1) return false; n>>=1; } if(c==1) return true; else return false; } };
leetcode_Power of Two_easy
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。