本文是在学习中的总结,欢迎转载但请注明出处: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 valuepublic 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 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  2. 万能头文件#include<bits/stdc++.h>更新GCC10.2.0版本

    C++标准库里的万能头文件:#include<bits/stdc++.h> 可用于各大Online Judge测试平台(POJ除外,这些年不维护更新了) 由于网上的都是2014年版的万能头 ...

  3. C++预编译头文件 bits/stdc++.h

    有时候会看到别人包含这样的头文件: #include "bits/stdc++.h" 这个头文件中有很多预先包含的头文件,内容如下: // C++ includes used fo ...

  4. 解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed

    解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed 参考文章: (1)解决wi ...

  5. 领扣-191 位1的个数 Number of 1 Bits MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

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

  7. 高级c++头文件bits/stdc++.h

    用这种方法声明头文件只需两行代码 #include<bits/stdc++.h> using namespace std; 这个头文件包含以下等等C++中包含的所有头文件: #includ ...

  8. Jan 09 - Number of 1 Bits; Bit Operation;

    和 reverse Bits同一类型 代码: public class Solution { // you need treat n as an unsigned value public int r ...

  9. 【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...

    文章目录 一. 报错信息 二.解决方案 ( 检查镜像地址环境变量设置 ) 1.检查镜像地址环境变量设置 ( 错误方案 没有解决问题 ) 2.尝试使用官方地址 三.总结 1.官方镜像 ( 推荐方案 ) ...

最新文章

  1. Go 中 time.Parse 报错:year/month/day hour/minute/second out of range 时间格式化为什么是 2006-01-02 15:04:05?
  2. iPhone开源系列:iDev Recipes
  3. Intel不争气,7nm再延后两年将彻底落败
  4. MySQL not exists 真的不走索引么?
  5. java虚拟机内存监控_java虚拟机内存监控工具jps,jinfo,Jstack,jstat,jmap,jhat使用...
  6. Git基础知识教程整理(Git基本操作)
  7. 电脑打字手指正确姿势_洞箫的演奏姿势和动作
  8. 经典面试题(35):以下代码将输出的结果是什么?
  9. er图用什么软件_工艺流程图用什么软件做?规范实用的流程图工具
  10. jquery操作radio,checkbox
  11. u8文件服务器在哪设置,u8 设置文件服务器
  12. 屏幕录像专家 V2013 + 注册机(屏幕录像)
  13. mysql语句中单引号、双引号、反引号用法与区别
  14. Unregistering application *** with eureka with status DOWN
  15. SuperMap iDesktop许可模块介绍
  16. js 解决Safari浏览器中实现支付宝网页支付无法拉取支付宝APP的问题
  17. 你的性格是什么颜色——记录乐嘉的性格色彩分析(附性格色彩自测题
  18. vmware horizon桌面云部署
  19. GPS/BDS:星历表、历书、GPS三种启动方式
  20. 图嵌入/图神经网络模型整理归类

热门文章

  1. 正则表达式 匹配任意长度的字符
  2. 年初五,迎财神 | 一张码如何实现多渠道(微信、支付宝、云闪付...)收款
  3. 如何写一个高逼格 README
  4. vue后台管理upload(图片上传)
  5. dropna()函数
  6. 【科普】浅谈NB-IoT
  7. 关于单片机上的BOOT0和BOOT1
  8. 简单元胞自动机实现—Python
  9. HTML中6种空格标记
  10. 推荐系统学习笔记之三 LFM (Latent Factor Model) 隐因子模型 + SVD (singular value decomposition) 奇异值分解