位处理:191. Number of 1 Bits&&190. Reverse Bits&&7. Reverse Integer
2017-02-26 12:46
141 查看
191
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
class Solution {
public:
int hammingWeight(uint32_t n) {
int res=0;
while(n){
n&=n-1;//each loop we remove a 1 from n, 因为n和n-1只差一个1
++res;
}
return res;
}
};
190.
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
if(!n) return 0;
int res=0;
for(int i=0; i!=32; ++i){
res<<=1;
if(n&1) res++;
n>>=1;
}
return res;
}
};
这种题的共性是什么,一定要判断n是否为0.
7.
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
这题其实和reverse bits思路一致,都是先取出个位数,然后加到res上,res每轮*2或者*10,这样最后个位数就会变到最高位数上,而原先最高的位数随着原先数字每轮/2或者/10最后变成个位数。但是要注意x<0以及res超出int表示范围的情况。
class Solution {
public:
int reverse(int x) {
if(x==0) return 0;
int n=1;
if(x<0) {x*=-1; n=-1;}
long int res=0;
while(x){
res*=10;
res+=x%10;
x/=10;
}
if(res>INT_MAX||res<INT_MIN) return 0;
return res*n;
}
};
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
class Solution {
public:
int hammingWeight(uint32_t n) {
int res=0;
while(n){
n&=n-1;//each loop we remove a 1 from n, 因为n和n-1只差一个1
++res;
}
return res;
}
};
190.
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
if(!n) return 0;
int res=0;
for(int i=0; i!=32; ++i){
res<<=1;
if(n&1) res++;
n>>=1;
}
return res;
}
};
这种题的共性是什么,一定要判断n是否为0.
7.
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
这题其实和reverse bits思路一致,都是先取出个位数,然后加到res上,res每轮*2或者*10,这样最后个位数就会变到最高位数上,而原先最高的位数随着原先数字每轮/2或者/10最后变成个位数。但是要注意x<0以及res超出int表示范围的情况。
class Solution {
public:
int reverse(int x) {
if(x==0) return 0;
int n=1;
if(x<0) {x*=-1; n=-1;}
long int res=0;
while(x){
res*=10;
res+=x%10;
x/=10;
}
if(res>INT_MAX||res<INT_MIN) return 0;
return res*n;
}
};
相关文章推荐
- 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits
- Bit Manipulation-----190. Reverse Bits&&191. Number of 1 Bits
- [LeetCode刷题记录]190-191 Number of 1 Bits & Reverse Bits
- 338. Counting Bits && 191. Number of 1 Bits
- 191. Number of 1 Bits&二进制中1的个数
- <LeetCode OJ>Reverse Bits【190】
- (LeetCode) 191. Number of 1 Bits
- AT&T汇编处理字符串
- 处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
- 金融科技&大数据产品推荐:氪信XBehavior — 基于高维行为语言处理技术的信贷风险评估
- 求解xilinx ISE使用报错的处理——Port <clk> has illegal connections. This port is connected to an input buffer
- 图像处理算法3——Fisher's linear discriminant
- Oracle的SQL语句中如何处理‘&’符号
- leetcode_191. Number of 1 Bits 二进制表示中1的个数 ,位运算的应用,按位与&
- LeetCode191. Number of 1 Bits
- 字符串处理函数(2)replace&enumerate
- __autoload && set_hander php的错误异常处理机制以及自动加载机制
- ora-12154 TNS:"无法处理服务名"的一个解决方法(转)
- C++使用try&catch进行异常处理的简单范例
- C语言错误处理——setjmp & longjmp