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

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

Leetcode: Number of 1 Bits

2019-11-14 23:16:58
字體:
來源:轉載
供稿:網友
Leetcode: Number of 1 Bits
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. 我用的 rightshift 是>>>, 是unsigned right shift

2. & 的優先級不如 ==, 所以要把&的兩個參數括起來

 1 public class Solution { 2     // you need to treat n as an unsigned value 3     public int hammingWeight(int n) { 4         int count = 0; 5         for (int i=0; i<32; i++) { 6             if ((n>>>i & 1) == 1) count++; 7         } 8         return count; 9     }10 }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 锦州市| 伊金霍洛旗| 都江堰市| 正蓝旗| 荣成市| 牡丹江市| 五常市| 盐山县| 霍林郭勒市| 芜湖市| 长阳| 龙岩市| 乐平市| 鄂伦春自治旗| 庄浪县| 叙永县| 连州市| 砚山县| 格尔木市| 大悟县| 巴青县| 马山县| 长阳| 滁州市| 大庆市| 万安县| 新田县| 云和县| 广元市| 东乌珠穆沁旗| 德钦县| 增城市| 江北区| 汕头市| 昆山市| 明星| 明星| 日喀则市| 通辽市| 田林县| 五华县|