2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits
代码均测试通过!
1.Number of 1 Bits
本题收获:
1.Hamming weight:即二进制中1的个数
2.n &= (n-1)【n = n & (n-1)】的用处
题目:
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.
notice:输入的数字是十进制,是对十进制进行判断
思路:
我的思路:
遍历字符串1的个数,(疑问,数字转换成字符串的具体操作)
leetcode/discuss思路:
n &= n-1,将二进制n与n-1按位与,有几个1将循环几次。这种方法效率最高。
代码1:将整型数转化为二进制数后其中的1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = ;
while (n)
{
n &= (n - ); //即为n = n&n(n-1),联想n += 1为n = n+1
count++;
}
return count;
}
};
我的代码:带有main函数
#include "stdafx.h"
#include "iostream"
#include "stdint.h" //uint32_t的头函数
using namespace std; class MyClass
{
public:
int hanminweight(uint32_t n)
{
int count = ; while(n != )
{
n = n & (n-);
count++;
//cout << count << endl;测试
//cout << n << endl;
}
return count;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
MyClass solution;
int m;
uint32_t nums;
cin >> nums ;
m = solution.hanminweight(nums);
cout << "the number of 1bits is:" << m << endl;
//cin.get(); //不知道为什么cin.get()不可以了??
system("pause");
return ;
}
代码2:开始讲题意理解错了,误以为输入为二进制,现在重新写了个输入为二进制的代码。
#include "stdafx.h"
#include "iostream"
#include <string> //string的头文件
#include "stdint.h" //uint32_t的头函数,这里可以不用
using namespace std; class Binary
{
public:
int hanminweightbinary(string s)
{
int nums = ;
for (size_t i = ; i < s.size(); i++)
{
//cout << s[i] << endl; //测试
if (s[i] == '') //在字符串中判断时数字为整型1 应该为‘1’此为字符串,if (s[i] == 1) 出错,nums始终为0
{
++nums;
}
//cout << nums << endl;
}
return nums;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
Binary solution; //若想要多次循环可以加while(true)语句
int m;
string str;
cin >> str;
m = solution.hanminweightbinary(str);
cout << "the number of 1bits is:" << m << endl;
//cin.get(); //不知道为什么cin.get()不可以了??
system("pause");
return ;
}
调试过程中出错:
错误1:error C2679: 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
原因:没有加string的头文件#include <string>
错误2:nums的值始终为0
原因:if (s[i] == 1) 应该为 if (s[i] == '1')
碎碎念(不清楚的知识点):
一。n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子:
n = 10100(二进制),则(n-1) = 10011 ==》n&(n-1) = 10000
可以看到原本最低位为1的那位变为0。
弄明白了n&(n-1)的作用,那它有哪些应用?
1). 求某一个数的二进制表示中1的个数
while (n >0 )
{
count ++;
n &= (n-1);
}
2). 判断一个数是否是2的方幂
n > 0 && ((n & (n - 1)) == 0 )
二.to_string是将整型转换成字符串型,具体怎么用还不清楚:http://www.cplusplus.com/reference/string/to_string/可以参考这个链接
三.运算符
按位与运算符(&)
参加运算的两个数据,按二进制位进行“与”运算。
运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;
即:两位同时为“1”,结果才为“1”,否则为0。
例如:3&5 即 0000 0011 & 0000 0101 = 0000 0001 因此,3&5的值得1。
负数按补码形式参加按位与运算。
“与运算”的特殊用途:
(1)清零。如果想将一个单元清零,即使其全部二进制位为0,只要与一个各位都为零的数值相与,结果为零。
(2)取一个数中指定位
方法:找一个数,对应X要取的位,该数的对应位为1,其余位为零,此数与X进行“与运算”可以得到X中的指定位。
例:设X=10101110,
取X的低4位,用 X & 0000 1111 = 0000 1110 即可得到;
还可用来取X的2、4、6位。
按位或运算符(|)
参加运算的两个对象,按二进制位进行“或”运算。
运算规则:0|0=0; 0|1=1; 1|0=1; 1|1=1;
即 :参加运算的两个对象只要有一个为1,其值为1。
例如:3|5 即 0000 0011 | 0000 0101 = 0000 0111 因此,3|5的值得7。
负数按补码形式参加按位或运算。
“或运算”特殊作用:
(1)常用来对一个数据的某些位置1。
方法:找到一个数,对应X要置1的位,该数的对应位为1,其余位为零。此数与X相或可使X中的某些位置1。
例:将X=10100000的低4位置1 ,用 X | 0000 1111 = 1010 1111即可得到。
异或运算符(^)
参加运算的两个数据,按二进制位进行“异或”运算。
运算规则:0^0=0; 0^1=1; 1^0=1; 1^1=0;
即:参加运算的两个对象,如果两个相应位为“异”(值不同),则该位结果为1,否则为0。
“异或运算”的特殊作用:
(1)使特定位翻转 找一个数,对应X要翻转的各位,该数的对应位为1,其余位为零,此数与X对应位异或即可。
例:X=10101110,使X低4位翻转,用X ^ 0000 1111 = 1010 0001即可得到。
(2)与0相异或,保留原值 ,X ^ 0000 0000 = 1010 1110。
取反运算符(~)
参加运算的一个数据,按二进制位进行“取反”运算。
运算规则:~1=0; ~0=1;
即:对一个二进制数按位取反,即将0变1,1变0。
使一个数的最低位为零,可以表示为:a&~1。
~1的值为1111111111111110,再按“与”运算,最低位一定为0。因为“~”运算符的优先级比算术运算符、关系运算符、逻辑运算符和其他运算符都高。
左移运算符(<<)
将一个运算对象的各二进制位全部左移若干位(左边的二进制位丢弃,右边补0)。
例:a = a << 2 将a的二进制位左移2位,右补0,
左移1位后a = a * 2;
若左移时舍弃的高位不包含1,则每左移一位,相当于该数乘以2。
右移运算符(>>)
将一个数的各二进制位全部右移若干位,正数左补0,负数左补1,右边丢弃。
操作数每右移一位相当于原操作数除2
例如:a = a >> 2 将a的二进制位右移2位,
左补0 or 补1 得看被移数是正还是负。
无符号右移运算符(>>>)
>> 运算符把 expression1 的所有位向右移 expression2 指定的位数。expression1 的符号位被用来填充右移后左边空出来的位。向右移出的位被丢弃。
例如,下面的代码被求值后,temp 的值是 -4:
-14 (即二进制的 11110010)右移两位等于 -4 (即二进制的 11111100)。
var temp = -14 >> 2
复合赋值运算符
位运算符与赋值运算符结合,组成新的复合赋值运算符,它们是:
&= 例:a &= b 相当于a=a & b
|= 例:a |= b 相当于a=a | b
>>= 例:a >>= b 相当于a=a >> b
<<= 例:a <<= b 相当于a=a << b
^= 例:a ^= b 相当于a=a ^ b
运算规则:和前面讲的复合赋值运算符的运算规则相似。
如果两个不同长度的数据进行位运算时,系统会将二者按右端对齐,左边补0,然后进行位运算。
2016.5.15——leetcode:Number of 1 Bits ,的更多相关文章
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
- [LeetCode] Number of 1 Bits 位操作
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode——Number of 1 Bits
//求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...
- LeetCode Number of 1 Bits 计算1的个数
题意: 提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数. 思路: 考察二进制的特性,设有k个1,则复杂度为O(k).考虑将当前的数n和n-1做按位与,就会将n的最后一个1去 ...
- [LeetCode] Number of Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...
随机推荐
- Java 软引用和弱引用
软引用(SoftReference) 软引用是用来描述一些有用但并不是必需的对象,在Java中用java.lang.ref.SoftReference类来表示.对于软引用关联着的对象,只有在内存不足的 ...
- Jackson 使用
// 序列化出来的 JSON, 不包含值为 NULL 类型字段. mapper.setSerializationInclusion(Include.NON_NULL); Jackson provide ...
- oracle 空表导出dmp会报错
步骤一.查找出来的语句全部复制并执行 select 'analyze table '||table_name||' compute statistics;' from user_tables; 步骤二 ...
- 百度地图经纬度批量查找功能XGeocoding使用手册
<XGeocoding使用手册> 1.下载XGeocoding V2 http://www.gpsspg.com/xgeocoding/download/ 2.解压XGeocoding_v ...
- 51nod 1471 小S的兴趣 | 分块 链表
51nod 1471 小S的兴趣 题面 小S喜欢有趣的事.但是,每个人的兴趣都是独特的.小S热衷于自问自答.有一天,小S想出了一个问题. 有一个包含n个正整数的数组a和针对这个数组的几个问题.这些问题 ...
- 洛谷P1592 互质
题目描述 输入两个正整数n和k,求与n互质的第k个正整数. 输入输出格式 输入格式: 仅一行,为两个正整数n(≤10^6)和k(≤10^8). 输出格式: 一个正整数,表示与n互质的第k个正整数. 由 ...
- 洛谷 P2751 [USACO4.2]工序安排Job Processing 解题报告
P2751 [USACO4.2]工序安排Job Processing 题目描述 一家工厂的流水线正在生产一种产品,这需要两种操作:操作A和操作B.每个操作只有一些机器能够完成. 上图显示了按照下述方式 ...
- Linux使用vim进行多文件查找和替换的方法
vim是Linux系统下常用的文本编辑,通过使用多种shell命令能够实现多文件的查找和替换,那么具体会使用到那些命令呢?下面小编就给大家介绍下Linux系统vim多文件查找和替换的方法. 在linu ...
- GridView中网络图片延迟加载导致高度计算失败的问题
在使用下拉刷新以及加载更多控件的时候,出现了列表上滚不完的现象,经过半天的分析,最后得出结论:由于图片采用了延迟加载,导致列表按照没有加载图片时候的大小进行布局,相关的加载更多控件也就傻逼了. 最终解 ...
- 2016-2017 National Taiwan University World Final Team Selection Contest (Codeforces Gym) 部分题解
D 考虑每个点被删除时其他点对它的贡献,然后发现要求出距离为1~k的点对有多少个. 树分治+FFT.分治时把所有点放一起做一遍FFT,然后减去把每棵子树单独做FFT求出来的值. 复杂度$nlog^ ...