(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646201",
container: s
});
})();
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646162",
container: s
});
})();
二进制中1的个数
12329 人参与 2019年03月15日 10:20 分类 : 面试笔试 评论
题目描述
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
时间限制:1秒 空间限制:32768K
直接上代码吧:
int NumberOf1(int n) {
int i;
int num=0;
for(i=0;i<32;i++)
{
if((n&(1<<i))!=0)
num++;
}
return num;
}
代码很简单,就是按位检查是否为1,如果是,就计数。
绝对最佳答案及分析:
int NumberOf1(int n) {
int count = 0;
while(n!= 0){
count++;
n = n & (n - 1);
}
return count;
}
答案正确:恭喜!您提交的程序通过了所有的测试用例
分析一下代码:这段小小的代码,很是巧妙。
如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。
举个例子:一个二进制数1100,从右边数起第三位是处于最右边的一个1。减去1后,第三位变成0,它后面的两位0变成了1,而前面的1保持不变,因此得到的结果是1011.我们发现减1的结果是把最右边的一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算,从原来整数最右边一个1那一位开始所有位都会变成0。如1100&1011=1000.也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。
python代码实现如下:
# -*- coding:utf-8 -*-
class Solution:
def NumberOf1(self, n):
# write code here
count = 0
if n < 0:
n = n & 0xffffffff
while n:
count += 1
n = (n - 1) & n
return count
另类解法:
首先判断n是不是负数,当n为负数的时候,直接用后面的
while
循环会导致死循环,因为负数
向左移位的话最高位补
1
! 因此需要一点点特殊操作,可以将最高位的符号位
1
变成
0
,也就
是n &
0x7FFFFFFF
,这样就把负数转化成正数了,唯一差别就是最高位由
1
变成
0
,因为少了
一个
1
,所以count加
1
。之后再按照
while
循环里处理正数的方法来操作就可以啦!
最基本最简单的解法:
//超级简单容易理解 //&(与)
//
//把这个数逐次 右移 然后和1 与,
//就得到最低位的情况,其他位都为0,
//如果最低位是0和1与 之后依旧
是0,如果是1,与之后还是1。
//对于32位的整数 这样移动32次 就记录了这个数二进制中1的个数了
class Solution {
public:
int NumberOf1(int n) {
int count=0;
for(int i=0;i<32;i++){
if(n>>i&1)
count++;
}
return count;
}
};
来源:我是码农,转载请保留出处和链接!
本文链接:http://www.54manong.com/?id=1231
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646208",
container: s
});
})();
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646147",
container: s
});
})();
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
程序员面试 微信号:qq444848023 QQ号:444848023
加入【我是码农】QQ群:864689844(加群验证:我是码农)
<< 上一篇
下一篇 >>
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646186",
container: s
});
})();
(function() {
var s = "_" + Math.random().toString(36).slice(2);
document.write('
');
(window.slotbydup = window.slotbydup || []).push({
id: "u3646175",
container: s
});
})();
jQuery(document).ready(function($){
/* prepend menu icon */
$('#daohangs-around').prepend('
');
/* toggle nav */
$("#caidan-tubiao").on("click", function(){
$("#daohangs").slideToggle();
$(this).toggleClass("active");
});
});
评论专区