位运算相关 三道题

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. apt-get正在等待报头(waiting for headers)

    可能的解决方法 1. 删除/var/cache/apt/archives/下的所有文件.可能是上次没有成功导致遗留了部分文件. 2. 如果使用的是代理,需要检查DNS.如果机器不能连接DNS服务器,要 ...

  2. Eureka Instance实例信息配置

    Eureka包含四个部分的配置 instance:当前Eureka Instance实例信息配置 client:Eureka Client客户端特性配置 server:Eureka Server注册中 ...

  3. String、StringBuffer和StringBuilder源码解析

    1.String 1.1类的定义 public final class String implements java.io.Serializable, Comparable<String> ...

  4. python 搜索匹配文件目录下所有的jpg或者png图片

    import glob PATH_TO_TEST_IMAGES_DIR = 'D:\TrainChangeFifteenData\Picture\Test' for pidImage in glob. ...

  5. AGC035D

    AGC035D Add and Remove 题意 给出\(n\)个数,每次删除一个不在两端的数,然后把它的权值加到相邻的两个数上. 问操作\(n-2\)次后,所剩的两数之和的最小值 \(n\le18 ...

  6. c# 调用7za.exe执行压缩命令

    string path7z = $"7zsource\\{project.name}"; string path7zip = $"7z\\{project.name}.7 ...

  7. Eclipse:Eclipse所有发布版本的下载地址

    官方镜像:http://eclipse.mirror.rafal.ca/technology/epp/downloads/release/ 国内镜像:http://mirrors.neusoft.ed ...

  8. 如约而至(walk)

    LCA大佬的做法: 考虑暴力的高斯消元,我们优化它. $\sum\limits_{j} gcd(i,j)^{c-d} i^d j^d x_j=b_i$ $\sum\limits_{j} gcd(i,j ...

  9. TZ_01MyBatis_SqlMapConfig.xml

    1.sqlMapConfig的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE conf ...

  10. css3正方体效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...