题目描述

编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

示例 :

输入: 11
输出: 3
解释: 整数 11 的二进制表示为 00000000000000000000000000001011

示例 2:

输入: 128
输出: 1
解释: 整数 128 的二进制表示为 00000000000000000000000010000000

思路

思路一:

用Integer.bitCount函数统计参数n转成2进制后有多少个1

    public static int bitCount(int i) {
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}

思路二:

如果一个整数不为0,那么这个整数至少有一位是1。

如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。

其余所有位将不会受到影响。

思路三:

用flag来与n的每位做位于运算,来判断1的个数

代码实现

package BitManipulation;

/**
* 191. Number of 1 Bits(位1的个数)
* 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
*/
public class Solution191 {
public static void main(String[] args) {
Solution191 solution191 = new Solution191();
int n = 11;
System.out.println(solution191.hammingWeight(n));
} /**
* 用Integer.bitCount函数统计参数n转成2进制后有多少个1
*
* @param n
* @return
*/
public int hammingWeight(int n) {
return Integer.bitCount(n);
} /**
* 如果一个整数不为0,那么这个整数至少有一位是1。
* 如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。
* 其余所有位将不会受到影响。
*
* @param n
* @return
*/
public int hammingWeight_2(int n) {
int count = 0;
while (n != 0) {
count++;
n = (n - 1) & n;
}
return count;
} /**
* 用flag来与n的每位做位于运算,来判断1的个数
*
* @param n
* @return
*/
public int hammingWeight_3(int n) {
int count = 0;
int flag = 1;
while (flag != 0) {
if ((flag & n) != 0) {
count++;
}
flag = flag << 1;
}
return count;
}
}

Leetcode#191. Number of 1 Bits(位1的个数)的更多相关文章

  1. [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

    描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...

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

  3. [LeetCode] 191. 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. 191 Number of 1 Bits 位1的个数

    编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量).例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以 ...

  5. LN : leetcode 191 Number of 1 Bits

    lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...

  6. LeetCode 191. Number of 1 bits (位1的数量)

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

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

  8. LeetCode 191 Number of 1 Bits

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

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

随机推荐

  1. [Oracle维护工程师手记]一次升级后运行变慢的分析

    客户报告,当他从 Oracle 11.1.0.7 ,迁移到云环境,并且升级到12.1.0.2.运行客户的应用程序测试,发现比以前更慢了. 从AWR report 的"Top 10 Foreg ...

  2. PS制作简洁漂亮的立体抽丝文字

    一.新建一个800*600px文档,并将Background图层创建一个副本,将其命名为Background_copy. 二.双击Background_copy图层,勾选渐变叠加,并设定以下数值 勾选 ...

  3. git修改已push的commit信息

    本条适用于修改已push的最新的commit信息,确保本地的文件是最新的. 使用 git commit --amend 命令,(修改最近一次提交的注释信息),会进入到vim 编辑器 编辑提交信息,保存 ...

  4. Flask —— 信号(5)

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. pip3 install blinker 1. 内置信号 request_started = ...

  5. react 报错的堆栈处理

    react报错 Warning: You cannot PUSH the same path using hash history 在Link上使用replace 原文地址https://reactt ...

  6. MySQL中的float和decimal类型有什么区别

    decimal 类型可以精确地表示非常大或非常精确的小数.大至 1028(正或负)以及有效位数多达 28 位的数字可以作为 decimal类型存储而不失其精确性.该类型对于必须避免舍入错误的应用程序( ...

  7. 使用jquery中$.each()方法来循环一个数据列表

    定义和用法 jQuery.each() 函数用于遍历指定的对象和数组. 语法 $.each( object, callback ) 参数 描述 object Object类型 指定需要遍历的对象或数组 ...

  8. Python学习之路——函数对象作用域名称空间

    一.函数对象 # 函数名就是存放了函数的内存地址,存放了内存地址的变量都是对象,即 函数名 就是 函数对象 # 函数对象的应用 # 1 可以直接被引用 fn = cp_fn # 2 可以当作函数参数传 ...

  9. [SimplePlayer] 6. 音频同步

    音频的同步并不需要我们在程序实现.在设置好声道.采样率.音频格式后,程序只需要保证能一直提供音频数据就行,其余工作基本都由声卡实现.

  10. JavaScript- BOM, DOM

    BOM Browser Object Model 浏览器对象模型, 提供与浏览器窗口进行交互的方法 它使 JavaScript 有能力与浏览器进行“对话”. BOM 最主要的对象就是 window 对 ...