国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

191. Number of 1 Bits

2019-11-08 02:37:31
字體:
來源:轉載
供稿:網友

題目

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.

Credits: Special thanks to @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.


思路

n&(n-1)會把n最后的一個bit為1的改成0,循環遍歷計算bit 1的個數


代碼

class Solution {public: int hammingWeight(uint32_t n) { int sum = 0; while(n) { sum++; n &= (n-1); } return sum; }};
上一篇:198. House Robber

下一篇:189. Rotate Array

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 招远市| 岚皋县| 襄樊市| 霍城县| 马尔康县| 莱阳市| 视频| 灵川县| 自治县| 确山县| 正定县| 南宁市| 依兰县| 仙游县| 喀喇| 镇沅| 松潘县| 新竹市| 基隆市| 道孚县| 宾川县| 长顺县| 海丰县| 东山县| 麦盖提县| 县级市| 泾源县| 沂水县| 阳高县| 夹江县| 青龙| 多伦县| 东山县| 罗源县| 闵行区| 栖霞市| 太白县| 鲁山县| 郎溪县| 乡宁县| 错那县|