本文是在学习中的总结。欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/44486547

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.

思路:

(1)题意为给定一个无符号数。返回其相应二进制数中1的个数。

(2)该题主要考察进制的转换操作。java中没有无符号数字(为什么没有能够百度下)。无符号数即给定的数都是非负数。这样我们能够通过java自带类库中的Integer.toBinaryString(value)将指定整数专为二进制数。然后求得二进制数中包括1的个数就可以。详情见下方代码。

(3)希望本文对你有所帮助。

算法代码实现例如以下:

	/**
* @param liqq 直接用类库
*/
public int hammingWeight(int value) {
int count = 0;
String binaryString = Integer.toBinaryString(value);
for (int i = 0; i < binaryString.length(); i++) {
char charAt = binaryString.charAt(i);
if (charAt == '1') {
count++;
}
}
return count;
}
	/**
* @param liqq 简化类库中toBinaryString方法的使用
*/
// you need to treat n as an unsigned value
public int hammingWeight(int value) {
int count = 0;
// String binaryString = Integer.toBinaryString(value);
char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z' };
char[] buf = new char[32];
int charPos = 32;
int radix = 2;
int mask = 1;
do {
buf[--charPos] = digits[value & 1];
value >>>= 1;
} while (value != 0); String binaryString = new String(buf, charPos, (32 - charPos)); for (int i = 0; i < binaryString.length(); i++) {
char charAt = binaryString.charAt(i);
if (charAt == '1') {
count++;
}
} return count;
}

Leetcode_191_Number of 1 Bits的更多相关文章

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

  2. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  3. 【leetcode】Number of 1 Bits

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

  4. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  5. CodeForces 485C Bits[贪心 二进制]

    C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...

  6. uva12545 Bits Equalizer

    uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...

  7. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  8. Number of 1 Bits(Difficulty: Easy)

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

  9. 解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译)

    解剖SQLSERVER 第五篇  OrcaMDF里读取Bits类型数据(译) http://improve.dk/reading-bits-in-orcamdf/ Bits类型的存储跟SQLSERVE ...

随机推荐

  1. 让动态创建的ActiveX控件响应Windows消息

    当我们通过 CWnd::CreateControl() 动态创建 ActiveX   控件时, Windows 消息并不会被发送给我 们的由   CWnd 派生得控件类.例如,即使我们为 WM_KIL ...

  2. [Usaco2008 Feb]Meteor Shower流星雨

    去年偶们湖南遭受N年不遇到冰冻灾害,现在芙蓉哥哥则听说另一个骇人听闻的消息: 一场流星雨即将袭击整个霸中,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽, 届时将会对它撞到的一切东西造成毁灭性的打击 ...

  3. 获取option中间的值

    <select name="wytype" id="wytype"> <option value="">—请选择—& ...

  4. (2)入门指南——(7)添加jquery代码(Adding our jQuery code)

    Our custom code will go in the second, currently empty, JavaScript file which we included from the H ...

  5. ThinkPHP 3.2 开发过程

    原文:ThinkPHP 3.2 开发过程 设置所有项目的公共配置Application\Common\Conf\config.php,SAE模式下配置文件为config_sae.php 配置默认的模块 ...

  6. ubuntu 64位下安装wps

    首先,去官网下载wps.地址:http://community.wps.cn/download/ 然后切换到下载文件夹.运行例如以下命令: $ sudo apt-get install ia32-li ...

  7. GMM的EM算法

    在聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut一文中我们给出了GMM算法的基本模型与似然函数,在EM算法原理中对EM算法的实现与收敛性证明 ...

  8. linux通过使用mail发送电子邮件

    通过外部方法发送的电子邮件 bin/mail默认为本地sendmail发送电子邮件,求本地的机器必须安装和启动Sendmail服务.配置很麻烦,并且会带来不必要的 资源占用.而通过改动配置文件能够使用 ...

  9. UVA 10892 LCM Cardinality(数论 质因数分解)

    LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of number ...

  10. 三框架:使用数据源dbcp注意

    使用spring整合hibernate时间,需要使用该数据源,数据源使用apache的dbcp,使用dbcp当需要依靠pool的jar包.选择dbcp和pool当你需要注意. DBCP 2 compi ...