LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
位运算相关 三道题
231. Power of Two
Given an integer, write a function to determine if it is a power of two. (Easy)
分析:
数字相关题有的可以考虑用位运算,例如&可以作为筛选器。
比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0)
代码:
class Solution {
public:
bool isPowerOfTwo(int n) {
if (n <= ) {
return false;
}
return ( (n & (n - )) == ); //按位筛选,考虑位操作
}
};
191. Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. (Easy)
分析:
同上一个题,n & n-1可以去掉n的最低位1,因此循环即可求出1的个数。
代码:
class Solution {
public:
int hammingWeight(uint32_t n) {
int num = ;
while (n > ) {
n = (n & (n - ));
num++;
}
return num;
}
};
342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. (Easy)
分析:
首先是4的幂肯定是2的幂,所以可以利用Power of Two, 其次考察 4, 16等数,其1出现在奇数位。
所以利用0101 &该数,可以删选掉是2的幂但不是4的幂。
代码:
class Solution {
public:
bool isPowerOfFour(int num) {
if (num <= ) {
return false;
}
return ( (num & (num - )) == && (num & 0x55555555) ); //有一个1,且1在奇数位上,&操作起到筛选器作用,5即0101
}
};
LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four的更多相关文章
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- [Swift]LeetCode191. 位1的个数 | Number of 1 Bits
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
- Java for LeetCode 191 Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
随机推荐
- windows2008解决必须使用角色管理工具安装或配置 NET3.5 sp1问题
win2008系统安装网站环境系统的时候常常提示:必须使用角色管理工具 安装或配置microsoft.net framework 3.5 sp1 ,导致安装不成功. 原因在于windows2008系统 ...
- [Day3] Nginx配置Https
一. 网络安全之Http与Https Http协议是互联网行业中设计的最好架构之一.20多年间,应用Http协议传输数据的软件越来越多,企图从http协议传输中非法获取.篡改用户重要数据的非法行为也越 ...
- MyEclipse6.5安装SVN插件方法
MyEclipse6.5安装SVN插件,掌握了几种方法,本节就像大家介绍一下MyEclipse6.5安装SVN插件的三种方法,看完本文你肯定有不少收获,希望本文能教会你更多东西. 一.安装方法: My ...
- stream的map用法
List<String> list = new ArrayList<>();list.add("1");list.add("2");li ...
- kindle电子书下载网站收藏
kindle推 http://readfree.me 鸠摩捜书 新浪爱问共享资料 我的小书屋 云海图书馆 书语者 36镇网站收藏夹
- return语句必须要注意的地方
先看下面程序: function foo1() { return { /*返回对象{}他留有一个大括号跟return在同一行*/ bar: "hello" }; } functio ...
- [转]js的键盘事件
类型 键盘事件用来描述键盘行为,主要有keydown.keypress.keyup三个事件 keydown 当用户按下键盘上的任意键时触发,如果按住不放的话,会重复触发该事件 <div id=& ...
- jnhs[未解决]无法使用选定的hibernate配置文件建立数据库连接.请验证hibernate.cfg.xml中的数据库连接详情信息
工程可以正常的使用读写数据库,当然model和model.hbm.xml文件是自己写的. 解决中
- 你应该知道的25个非常有用的CSS技巧
在我们的前端CSS编码当中,经常要设置特殊的字体效果,边框圆角等等,还要考虑兼容性的问题, CSS网页布局,说难,其实很简单. 说它容易,往往有很多问题困扰着新手,在中介绍了非常多的技巧,这些小技巧与 ...
- Linux下读写UART串口的代码
Linux下读写UART串口的代码,从IBM Developer network上拿来的东西,操作比較的复杂,就直接跳过了,好在代码能用,记录一下- 两个实用的函数- //////////////// ...