首页 > 其他 > 详细

LeetCode题解之Number of 1 Bits

时间:2018-07-11 17:33:54      阅读:191      评论:0      收藏:0      [点我收藏+]

1、题目描述

技术分享图片

2.问题分析

使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可。

3、代码

 1 int hammingWeight(uint32_t n) {
 2         bitset<32> b(n);
 3         string b_s = b.to_string() ;
 4         
 5         int count_one = 0;
 6         for(string::iterator it = b_s.begin(); it != b_s.end() ; ++it )
 7         {
 8             if( *it == 1)
 9                 ++count_one;
10         }
11         return count_one;
12     }

 

LeetCode题解之Number of 1 Bits

原文:https://www.cnblogs.com/wangxiaoyong/p/9295660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!