本文是在学习中的总结。欢迎转载但请注明出处: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. 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)

    在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...

  2. NIO框架之MINA源码解析(转)

    http://blog.csdn.net/column/details/nio-mina-source.html http://blog.csdn.net/chaofanwei/article/det ...

  3. POJ2421 & HDU1102 Constructing Roads(最小生成树)

    嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告 ...

  4. MingW环境下的windows编程

    一般在进行windows编程时都使用vc++精简版,其插入菜单,图片等资源等更简单,且vc中对中文有更好的支持,win7下安装的Mingw中文并不能很好地显示,有光标显示的位置和光标实际位置不符的问题 ...

  5. C++ 直方图匹配算法代码

    /*-------------------------------------------------------------------------*/ // 函数名称: histeq() // 传 ...

  6. linux expect, spawn用法小记

    linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...

  7. Iron Foundry

    Iron Foundry Provided by Tier 3 Iron Foundry is a project initiated by the engineers of Tier 3, an e ...

  8. DL动态载入框架技术文档

    DL动态载入框架技术文档 DL技术交流群:215680213 1. Android apk动态载入机制的研究 2. Android apk动态载入机制的研究(二):资源载入和activity生命周期管 ...

  9. JavaScript(15)jQuery 选择器

    jQuery 选择器 选择器同意对元素组或单个元素进行操作. jQuery 元素选择器和属性选择器同意通过标签名.属性名或内容对 HTML 元素进行选择. 在 HTML DOM 术语中:选择器同意对 ...

  10. zoj3795 Grouping --- 良好的沟通,寻找最长的公路

    给定一个图,为了保持图分裂至少成多个集合的集合内可以使点没有直接或间接的关系. 首先,题意可以得到图中可能含有环.该环的内侧是肯定是要被拆卸. 图点降低建DAG画画,能想象..图从零点渗透深入,在点中 ...