leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
题目:
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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
解法一:
此题关键是怎样推断一个数字的第i为是否为0 即: x& (1<<i)
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
for(int i = 0; i < 32; i++){
if((n & (1<<i)) != 0)count++;
}
return count;
}
};
解法二:此解关键在于明确n&(n-1)会n最后一位1消除,这样循环下去就能够求出n的位数中为1的个数
class Solution {
public:
int hammingWeight(uint32_t n) {
int count = 0;
while(n > 0){
n &= n-1;
count ++;
}
return count;
}
};
二:Bitwise AND
of Numbers Range
题目:
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
分析:此题提供两种解法:1:当m到n之前假设跨过了1,2,4,8等2^i次方的数字时(即推断m与n是否具有同样的最高位),则会为0,否则顺序将m到n相与。
解法二:利用上题中的思路。n&(n-1)会消除n中最后一个1,如1100000100当与n-1按位与时便会消除最后一个1,赋值给n(这样就减免了非常多不必要按位与的过程)
解法一:
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int bitm = 0, bitn = 0;
for(int i =0; i < 31; i++){
if(m & (1<<i))bitm = i;
if(n & (1<<i))bitn = i;
}
if(bitm == bitn){
int sum = m;
for(int i = m; i < n; i++) // 为了防止 2147483647+1 超过范围
sum = (sum & i);
sum = (sum & n);
return sum;
}
else return 0;
}
};
解法二:
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
while(n > m){
n &= n-1;
}
return n;
}
};
题目:
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle
which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 =
1
分析:此题关键是用一个set或者map来存储该数字是否已经出现过————hash_map+math
class Solution {
public:
bool isHappy(int n) {
while(n != 1){
if(hset.count(n)) return false; // 通过hashtable 推断是否出现过
hset.insert(n);
int sum = 0;
while(n != 0){ // 求元素的各个位置平方和
int mod = n%10;
n = n/10;
sum += mod * mod;
}
n = sum;
}
return true;
}
private:
set<int> hset;
};
题目:
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析:此题关键在于用到异或
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ans = 0;
for(int i = 0; i < nums.size(); i++)
ans ^= nums[i];
return ans;
}
};
leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number的更多相关文章
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...
- LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)
201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 201. Bitwise AND of Numbers Range -- 连续整数按位与的和
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode#201] Bitwise AND of Numbers Range
Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...
- [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0
https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...
- Java for LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
随机推荐
- ubuntu下配置安装PYQT4
apt-get安装(快) sudo apt-get install libxext6 libxext-dev libqt4-dev libqt4-gui libqt4-sql qt4-dev-tool ...
- c#中的static
1.C# 不支持静态局部变量(在方法范围内声明的变量). 2.static类一般用于与状态无关的类.那么,什么是与状态无关的类?我的理解是当一个类中没有属性,只有方法的的时候,就可以认为这个类是与状态 ...
- django rest_framework--入门教程
题设.如果官网DEMO能够正常跑起来请继续,如果不能请参考上一篇 1.新建MODEL 在数据库里添加相应的数据,可以使用命令 manage.py syncdb 这时候会建立对应的表 2.新建序列化方法 ...
- 我们一起学SASS
写在前面 sass大约是4年前(2011年)的新技术,sass官网有详细介绍,包括安装指南.学习教程.语法细节文档等等,很全面也很清晰 为什么有必要学sass?因为很多前端自动化工具都用sass,比如 ...
- Jquery最全过滤器总结
不管什么时候,总是有这么些时候:当我们使用jQuery的各种过滤器时,总是有那么几个记不牢,还要搜索一下或者翻翻手册!多少次想总结一下,最终都没总结,现在网上找到一篇总结的不错的,但是排版有点乱,本人 ...
- (译)开发优秀的虚拟现实体验:从开发I Expect You to Die中总结的六个要点
这篇文章是我从网上找来的,我觉得他非常详细的解释了VR发展的需求和必要.我认为通过这篇文章可以让大家了解VR. 译者写在最前: 来到追光动画有好几个月了,抱歉这段时间也没有什么文章与大家分享,我现在在 ...
- jpa 注解使用说明
1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="",catalog=&quo ...
- 有关HTML5 Video对象的ontimeupdate事件的问题
日前在做一个视频播放的页面,其中用到了HTML5的Video对象,这个是HTML5中新增的一个对象,支持多种不同格式的视频在线播放,功能比较强大,而且还扩展了许多事件,可以通过JavaScript脚本 ...
- [BTS] Error Can't update assemblies.
Removal of the assembly failed. Make sure that all items in the assembly you are trying to remove fu ...
- C++ 表达式
<C++ Primer 4th>读书摘要 C++ 提供了丰富的操作符,并定义操作数为内置类型时,这些操作符的含义.除此之外,C++ 还支持操作符重载,允许程序员自定义用于类类型时操作符的含 ...