leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two.
题解:一次一次除2来做的话,效率低。所以使用位运算的方法(左移一位相当于原数乘2)列出在int范围内的所有2的倍数,然后和n异或如果等于0,那么n就是它(两个数的异或为0,那么两个数就相等)。
注意:位运算的优先级比==的优先级低,要加括弧。
- class Solution {
- public:
- bool isPowerOfTwo(int n) {
- int a=;
- if(n<=){
- return false;
- }
- else{
- for(int i=;i<=;i++){
- if((n^a)==){
- return true;
- }
- else{
- a<<=;
- }
- }
- return false;
- }
- }
- };
更好的解法:一个数是2的倍数,那么它的二进制表示中只有一个1,其它都是0,所以如果n&(n-1)==0,那么n就是2的倍数(因为减1只会影响二进制表示中最靠右的1).
- class Solution {
- public:
- bool isPowerOfTwo(int n) {
- return (n>)&&((n&(n-))==);
- }
- };
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. ...
- [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: ...
- 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 78. 子集 C++(位运算和回溯法)
位运算 class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { ...
- 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 ☆(是否2 的幂)
描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,编写一个函数来判断它是否是 2 的 ...
- 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 @ ...
随机推荐
- cocos2d-x 2.x 支持多个方向屏幕翻转
主要改动 RootViewController.mm 的 supportedInterfaceOrientations 方法 1.四个方向 UIInterfaceOrientationMaskAll ...
- vptr
#include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...
- [CMD]重启电脑
https://zhidao.baidu.com/question/686086701903450132.html bat是批处理,可以调用关机命令关机. 制作方法如下: 打开记事本程序: 输入如下内 ...
- docker笔记一
docker概念介绍: docker 是一个装在linux上的普通的软件.利用docker的命令,可以创建一个带有linux操作系统的镜像文件,docker命令运行这个带的linux操作系的镜像文件, ...
- php自定义函数: 加密下载地址
function getdownurl($downurl, $extime = "3600", $serverid = 1) { if (empty($downurl)) { re ...
- [POI2006]SZK-Schools
[POI2006]SZK-Schools luogu #include<bits/stdc++.h> using namespace std; const int N=405,M=1e5+ ...
- OutOfMemoryError: Java heap space和GC overhead limit exceeded在Ant的Build.xml中的通用解决方式
这个仅仅是一点点经验,总结一下,当中前两个相应第一个Error.后两个相应第二个Error,假设heap space还不够.能够再改大些. <jvmarg value="-Xms512 ...
- linux 中解压与压缩 常用操作详细讲解
平时有时候 会在服务器进行一些文件的操作,比如安装一些服务与软件等等,都有解压操作,一般在 导出一些简单的服务器文件,也是先压缩后再导出,因此,在这里根据平时用到解压与压缩命令的频率来记录下: 1.最 ...
- ALV 表头 ADD_TEXT
[转自http://lz357502668.blog.163.com/blog/static/16496743201252891452493/] CALL METHOD valid_reference ...
- 变量动态选取资源ID
1.使用Resources 类的 getIdentifier方法 Resources res=getResources(); return res.getIdentifier(type ...