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 ...
随机推荐
- CImage类
CImage封装了DIB(设备无关位图)的功能,因而可以让我们能够处理每个位图像素.这里介绍GDI+和CImage的一般使用方法和技巧. TAG: GDI CImage 后处理 我们知道,Vi ...
- 关于iptables的u32匹配
前面一篇文章----阐释了iptables最新的bpf match,说它将多个matches并成了一个经过编译的解释型bytecode bpf match,早在bpf match之前,u32 matc ...
- 大话项目管理工具之Confluence篇
前言 前面的两篇文章,讲的都是代码层次上的项目管理工具.这篇文章打算介绍一下关于知识管理的工具,当然,它也属于项目管理中的一部分.说到知识管理,你想到了什么呢?织网?找节点?不错,这是个人的知识管理. ...
- 一int考虑什么类型的多少字节
一int表示的字节数? 问题是,我们经常得到的答案4. 但int究竟占多少个字节,却跟你的机器环境有关. As you can see, the typical data type sizes mat ...
- 打印class文件的Java编译器内部的版本号
当改变了jdk版本时,在编译java时,会遇到Unsupported major.minor version错误.错误信息如下 : Unsupported major.minor version 50 ...
- 辛星PHP教程之yii和ci教程已经写完,望与朋友们交流
记得有个朋友给我说,你写的PHP框架是不是过于少了.我感觉仅仅有一个thinkphp确实不好,于是就又写了下yii和ci的教程,事实上我之前是研究过这两个框架的,因此写起来也还算得心应手吧.估计接下 ...
- 136 - Ugly Numbers
Ugly Numbers Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...
- spring中bean的设计模式
默认的是单例的. 如果不想单例需要如下配置: <bean id="user" class="..." singleton="false" ...
- GitHub上最火的40个iOS开源项目
1. AFNetworking 在众多iOS开源项目中,AFNetworking可以称得上是最受开发者欢迎的库项目.AFNetworking是一个轻量级的iOS. Mac OS X网络通信类库,现在是 ...
- JavaScript(15)jQuery 选择器
jQuery 选择器 选择器同意对元素组或单个元素进行操作. jQuery 元素选择器和属性选择器同意通过标签名.属性名或内容对 HTML 元素进行选择. 在 HTML DOM 术语中:选择器同意对 ...