LeetCode 191. Number of 1 Bits
2016-04-05 02:22
323 查看
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
问题:写一个函数,求一个无符号int型数据中有多少个1
解析:这题可以用位运算来解决
For example, the 32-bit integer ’11' has binary representation
00000000000000000000000000001011, so the function should return 3.
问题:写一个函数,求一个无符号int型数据中有多少个1
解析:这题可以用位运算来解决
class Solution { public: int hammingWeight(uint32_t n) { unsigned cnt = 0; for (;n != 0;) { if (n & 1 > 0) ++cnt; n = n >> 1; } return cnt; } };
相关文章推荐
- 用OpenInventor实现的NeHe OpenGL教程-第四十二课
- 用OpenInventor实现的NeHe OpenGL教程-第四十一课
- django之创建第7-4个项目-配置views文件实现url传值
- ThinkPHP3.2.3学习笔记2---模型
- 用OpenInventor实现的NeHe OpenGL教程-第四十课
- 怎么在面试时回答java垃圾回收机制相关问题?
- 用OpenInventor实现的NeHe OpenGL教程-第三十九课
- 用OpenInventor实现的NeHe OpenGL教程-第三十八课
- 用OpenInventor实现的NeHe OpenGL教程-第三十六课
- 用OpenInventor实现的NeHe OpenGL教程-第三十七课
- LeetCode 198. House Robber
- 241. Different Ways to Add Parentheses
- 用OpenInventor实现的NeHe OpenGL教程-第三十五课
- 2016CampDay7problemF.Frank Sinatra
- 用OpenInventor实现的NeHe OpenGL教程-第三十三课
- 用OpenInventor实现的NeHe OpenGL教程-第三十四课
- 用OpenInventor实现的NeHe OpenGL教程-第三十二课
- 主成分分析与隐变量模型
- 用OpenInventor实现的NeHe OpenGL教程-第三十课
- 用OpenInventor实现的NeHe OpenGL教程-第三十一课