位运算相关 三道题

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的更多相关文章

  1. 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' ...

  2. [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 ...

  3. [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 ...

  4. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  5. Number of 1 Bits(Difficulty: Easy)

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  6. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  7. LeetCode Number of 1 Bits

    原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...

  8. 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 ...

  9. (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 ...

随机推荐

  1. 如何收缩Mysql的ibdata1文件

    ibdata1是MySQL数据库中一个数据文件了,你会发现它来越大了,下面我来介绍收缩Mysql的ibdata1文件大小方法. 如果你有使用InnoDB来存储你的Mysql表,使用默认设置应该会碰到个 ...

  2. ETH功能类

    <?php /** * Ethereum JSON-RPC interface * * See Ethereum API documentation for more information: ...

  3. h5文件简介

    h5文件是层次格式的第5代版本,用于存储科学数据的一种文件格式和库文件,由美国超级计算中心与应用中心研发的文件格式,用以存储和组织大规模数据. H5将文件结构简化成两个主要的对象类型: 1 数据集da ...

  4. TZ_05_Spring_annotation常见注解

    Spring常用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...

  5. selenium自动化方式爬取豆瓣热门电影

    爬取的代码如下: from selenium import webdriver from bs4 import BeautifulSoup import time #发送请求,获取响应 def get ...

  6. Hackerrank--String Function Calculation(后缀数组)

    题目链接 Jane loves string more than anything. She made a function related to the string some days ago a ...

  7. spring cloud深入学习(二)-----服务注册中心spring cloud eureka

    服务治理 主要用来实现各个微服务实例的自动化注册与发现,为啥需要这玩意呢?在一开始比如A系统调用B服务,可能通过手工维护B服务的实例,并且还得采用负载均衡等方式,这些全部都得需要手工维护,等后面系统越 ...

  8. vim 查找及替换

    #全文(%)查找(s)行首2个空格开头(/^ ), 替换(g)为无即删掉(//) :%s/^ //g #全文查找每行尾的2个空格,删除 :%s/ $//g

  9. vim 的列操作

    删除列1.光标定位到要操作的地方.2.CTRL+v 进入“可视 块”模式,选取这一列操作多少行.3.d 删除. 插入列插入操作的话知识稍有区别.例如我们在每一行前都插入"() ": ...

  10. stackless 安装

    1.下载源码 https://bitbucket.org/stackless-dev/stackless/wiki/Download 2.编译.安装.路径生效 apt-get install libr ...