【一天一道LeetCode】#231. Power of Two
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given an integer, write a function to determine if it is a power of two.
(二)解题
题目大意:判断一个数是不是2的n次方。
解题思路:从二进制位运算来看,2的n次方是诸如0001,0010,0100,1000….等数,所以采用位运算,n&(n-1)如果为0表示是2的n次方,否则不是。
class Solution {
public:
bool isPowerOfTwo(int n) {
return n<=0?false:!(n&(n-1));
}
};
【一天一道LeetCode】#231. Power of Two的更多相关文章
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...
- LN : leetcode 231 Power of Two
lc 231 Power of Two 231 Power of Two Given an integer, write a function to determine if it is a powe ...
- [LeetCode] 231. Power of Two 2的次方数
Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: ...
- LeetCode 231 Power of Two
Problem: Given an integer, write a function to determine if it is a power of two. Summary: 判断一个数n是不是 ...
- Leetcode 231 Power of Two 数论
同样是判断数是否是2的n次幂,同 Power of three class Solution { public: bool isPowerOfTwo(int n) { ) && ((( ...
- (easy)LeetCode 231.Power of Two
Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...
- Java [Leetcode 231]Power of Two
题目描述: Given an integer, write a function to determine if it is a power of two. 解题思路: 判断方法主要依据2的N次幂的特 ...
- [LeetCode] 231. Power of Two ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )
1.题目:原题链接 Given an integer, write a function to determine if it is a power of two. 给定一个整数,判断该整数是否是2的 ...
- leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...
随机推荐
- Python virtualenv 使用总结篇
一.virtualenv的安装 1.使用pip全局安装virtualenv,建议使用pip 1.3或更高版本,在1.3之前,pip没有通过SSL从PYPI下载. $ [sudo] pip instal ...
- sklearn.model_selection 的 train_test_split作用
train_test_split函数用于将数据划分为训练数据和测试数据. train_test_split是交叉验证中常用的函数,功能是从样本中随机的按比例选取train_data和test_data ...
- python中如何将生成等差数列和等比数列
在python库numpy 中提供了函数linspace和logspace函数用于生产等差数列和等比数列. 1.linspace函数生成等差数列 def linspace(start, sto ...
- C#+HtmlAgilityPack+Dapper走一波爬虫
最近因为公司业务需要,又有机会撸winform了,这次的需求是因为公司有项目申报的这块业务,项目申报前期需要关注政府发布的相关动态信息,政府部门网站过多,人工需要一个一个网站去浏览和查阅,有时候还会遗 ...
- ml-agent:Win10下环境安装
这是我看到的最全面最详细的ml-agent讲解.(只用于学习与知识分享,如有侵权,联系删除.谢谢!) 来自CodeGize的个人博客 .源链接:https://www.cnblogs.com/Code ...
- Map value类型不同的写法
Map value类型不同的写法 Map<String, Object> accountMap=new HashMap<String, Object>(); int userI ...
- 0. 迷之 -> 和 .
0. 迷之 -> 和 . 箭头(->):左边必须为指针: 点号(.):左边必须为实体. e.g.1 class class A{ public: play(); }; int main() ...
- 数据库4m10d作业
Create table student ( Sno char(15) primary key , Sname varchar(10) not null, Sage tinyint , Special ...
- OpenCv error :unresolved external symbol(链接库没有加上)
Error 如下:Linking...: error LNK2001: unresolved external symbol _cvDestroyWindow: error LNK2001: unre ...
- Centos 7安装MYSQL
1.下载RPM源 直接使用yum命令下载mysql来进行安装是不能成功的,安装过程会有问题,这里需要使用rpm命令来先进下载.下载路径为: http://dev.mysql.com/get/mysql ...