Leetcode_191_Number of 1 Bits
本文是在学习中的总结。欢迎转载但请注明出处: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的更多相关文章
- [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 ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- uva12545 Bits Equalizer
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- 解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译)
解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译) http://improve.dk/reading-bits-in-orcamdf/ Bits类型的存储跟SQLSERVE ...
随机推荐
- Java-对象多态性
class A { public void fun1() { System.out.println("<----A------>"); } public v ...
- flex中在basewidget中不能使用图表组件问题
参考 http://blog.sina.com.cn/s/blog_51e3d0e70101hljz.html
- ubuntu安装iscsi
ubuntu安装iscsi target端:apt-get install iscsitarget ubuntu安装iscsi initiator端:apt-get install open-iscs ...
- VC/MFC 当鼠标移到控件上时显示提示信息
VC/MFC 当鼠标移到控件上时显示提示信息 ToolTip是Win32中一个通用控件,MFC中为其生成了一个类CToolTipCtrl,总的说来其使用方法是较简单的,下面讲一下它的一般用法和高级用法 ...
- Swift - 带结果列表的搜索条(UISearchDisplayController)的用法
(注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...
- javascript (二) 事件
<script></script> 函数写法: function fun_name(){ x=docment.getElementById("demo") ...
- linux route命令的使用详解
route命令用于显示和操作IP路由表.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现.在Linux系统中,设置路由通常是 为了解决以下问题:该Linu ...
- android: WheelView组件(滑轮组件)的应用!
android前段组件中, 填表单,选择条目 的样式有很多, WheelView滚动组件为其中一种,如下图所示: 前两 ...
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
- BUG系列:转让startActivityForResult()&onActivityResult()没有反应
前天遇到了一个麻烦,还真是麻烦啊. 我使用startActivityForResult()&onActivityResult().由Activity-A 跳转到Activity-B 页面,然后 ...