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 ...
随机推荐
- 【DM642学习笔记九】XDS560仿真器 Can't Initialize Target CPU
以前用的瑞泰的ICETEK-5100USB仿真器,现在换成XDS560试了试,速度快多了.把720*576的图片在imgae中显示也只需要四五秒钟.而5100仿真器需要三四分钟. 仿真器驱动安好后,刚 ...
- JSP-案例-商品增删改
商品的增删改查 1显示 部分代码 Dao public List<Product> findAllProduct() throws SQLException { QueryRunner r ...
- storm-jdbc的使用
最近项目组分配到研究storm-jdbc用法 发现网上关于insert和query方法挺多的,但是自定义方法很少.而且用法上也挺多缺陷.在此自己总结记录一下 JdbcInsertBolt 的核心代码 ...
- 2019-7-9-Roslyn-如何在-Target-引用-xaml-防止文件没有编译
title author date CreateTime categories Roslyn 如何在 Target 引用 xaml 防止文件没有编译 lindexi 2019-07-09 17:16: ...
- Hadoop 单机安装配置
- c/c++输出保留小数
c语言中,用print可以有格式符号,例如想让a保留两位小数 float a; print( "%.2f", a); 注意这里如果a是0.1, 那么打印出来会自动补0,也就是结果显 ...
- Leetcode414Third Maximum Number第三大的数
给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...
- writing-mode,文字竖直书写,字符之间距离,单词之间距离
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- ES6--反引号的使用
/*动态初始退出登出框话模态框*/ /*动态的初始化退出登陆模态框 反引号ES6语法 * 为什么在使用字符串格式直接创建模态框 * 1.不能在html页面中创建模板,因为如果换一个页面就没有对应的模板 ...
- laravel-admin 自定义导出excel功能,并导出图片
https://www.jianshu.com/p/91975f66427d 最近用laravel-admin在做一个小项目,其中用到了excel导出功能. 但是laravel-admin自带的导出功 ...