位运算相关 三道题

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. 2019.10.25 csp-s模拟测试87 反思总结

    一次非常神奇的考试,考完试以后看着T2的0pts突然笑死我自己 太智障了这什么神奇的题意理解错误23333 T1一眼分类讨论,两眼二分,觉得分类讨论有点玄学但是出题人八成不会卡[何],然后本着对二分的 ...

  2. 【Streaming】Storm内部通信机制分析

    一.任务执行及通信的单元 Storm中关于任务执行及通信的三个概念:Worker(进程).Executor(线程)和Task(Spout.Bolt) 1.  一个worker进程执行的是一个Topol ...

  3. Javascript-new Date() 与 Date() 的区别

    var today1 = Date() //返回一个字符串(string),没有getDate等日期对象方法,内容为当前时间 var today2 = new Date() //返回一日期对象,内容为 ...

  4. WWDC 上讲到的 Objective C / LLVM 改进

    https://developer.apple.com/wwdc/videos/ Advances in Objective-C What's New in the LLVM Compiler 下面是 ...

  5. CoffeeScript 更优美的Javascript

    CoffeeScript 是一门编译到 JavaScript 的小巧语言. 在 Java 般笨拙的外表下, JavaScript 其实有着一颗华丽的心脏. CoffeeScript 尝试用简洁的方式展 ...

  6. jnhs-netbeans maven Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean) on project

    w 无法完成清理 出现这种错误,通常是由于启动了另一个tomcat 进程或者运行的javaw.exe进程,导致报错. 直接运行工程启动后再清理就好了 或者 重启大法

  7. TomCat 启动默认加载项目

    在最后加上这句代码即可:<Context path="" docBase="\项目名称" reloadable="true" cros ...

  8. 【python自动化学习笔记】

    [python自动化第一篇:python介绍与入门] [python自动化第二篇:python入门] [python自动化第三篇:python入门进阶]      [Python自动化第三篇(2):文 ...

  9. linux下对拍

    #!/bin/bash t=0; while true; do let "t = $t + 1" printf $t printf ":\n" ./rand & ...

  10. django 结合 bootstrap 使用

    git clone https://github.com/dyve/django-bootstrap3.git 要运行demo,需要在demo 中为其增加一个符号链接 bootstrap3 到上层目录 ...