【一天一道LeetCode】#191. Number of 1 Bits
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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的个数
解题思路:剑指offer上的老题了。
把一个整数减去1,然后与原整数做与运算,会把该整数最右边的一个1变成0,要计算1的个数,就循环上述操作,直到这个数为0为止。
class Solution {
public:
int hammingWeight(uint32_t n) {
int num = n;
int count = 0;
while(n)
{
n = n&(n-1);
count++;
}
return count;
}
};
【一天一道LeetCode】#191. Number of 1 Bits的更多相关文章
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- LeetCode 191. 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] 191. 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 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- Java for 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 ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
- Java [Leetcode 191]Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- 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 ...
随机推荐
- gcc编译器的工作流程
参考资料:http://www.cnblogs.com/dfcao/p/csapp_intr1_1-2.html 在linux系统上,从源文件到目标文件的转化是由编译器完成的.以hello.c程序的编 ...
- Mac上安装brew 包管理工具
Mac 上的包管理工具对于开发者来说是一件非常方便的工具,能够有效的对包进行管理. 所以这篇博客就来简单的讲一下brew 的安装和一些基础命令. brew 全称叫做Homebrew . 1. 首先来说 ...
- SSH上一个随笔的基础上添加上hibernate支持
配置文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...
- html文本encode后,js获取参数失败的bug
html中的空格encodeURIComponent后变成%C2%A0,而js中的空格是'%20',二者无法匹配,所以要进行一次替换
- SpringMVC与Zookeeper、Dubbo使用示例
Dubbo整合Zookeeper和spring示例程序 1.Dubbo架构 本篇文章基于dubbox,使用dubbo应该也可以正常运行. 我认为想讲清楚一个任何一个技术框架,首先熟悉架构是非常有必要的 ...
- python读取文本文件数据
本文要点刚要: (一)读文本文件格式的数据函数:read_csv,read_table 1.读不同分隔符的文本文件,用参数sep 2.读无字段名(表头)的文本文件 ,用参数names 3.为文本文件制 ...
- 转载c++常忘的知识点
C++的一些知识点比较零碎,下面清单的形式做一些记录与归纳,以供参考. 1.赋值操作符重载(深复制): (1)由于目标对象可能引用了以前的一些数据,所以应该先delete这些数据: (2)注意到对象可 ...
- centos 6安装 H3C iNode 上网客户端
我的安装目录是/usr/iNode 直接把客户端安装包拷到这个文件夹下然后解压: #rar x iNode2.-R0162.rar 然后进入文件夹,里边有一个install.sh文件,这是一个安装文件 ...
- Go 语言环境安装
Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Window 安装包下载地址为:https://golang.org/dl/. 各个系统对应的包名: 操 ...
- 【Java 语言】Java 多线程 一 ( 线程启动 | 线程中断 )
一. 线程启动 线程启动 : -- 1. 继承 Thread 运行线程 : 重写 Thread 类的 run 方法, 然后执行该线程; -- 2. 实现 Runnable 接口, 并运行线程; -- ...