国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 弋阳县| 原阳县| 遂昌县| 浦江县| 鹤岗市| 庄河市| 东乌珠穆沁旗| 同德县| 望江县| 乌海市| 贺兰县| 新泰市| 收藏| 芦溪县| 岳普湖县| 南乐县| 象州县| 文山县| 鄂温| 五大连池市| 乳山市| 南通市| 开鲁县| 罗城| 九龙坡区| 蒙自县| 巧家县| 五莲县| 建阳市| 介休市| 礼泉县| 靖安县| 临汾市| 广安市| 霍林郭勒市| 宜阳县| 格尔木市| 日土县| 福安市| 綦江县| 锦州市|