一:Number of 1 Bits

题目:

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;
}
};

三:Happy Number

题目:

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;
};

四:Single Number

题目:

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的更多相关文章

  1. 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 ...

  2. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...

  3. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  4. 201. Bitwise AND of Numbers Range -- 连续整数按位与的和

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  5. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

  6. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  7. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  8. LeetCode 201 Bitwise AND of Numbers Range 位运算 难度:0

    https://leetcode.com/problems/bitwise-and-of-numbers-range/ [n,m]区间的合取总值就是n,m对齐后前面一段相同的数位的值 比如 5:101 ...

  9. 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 ...

随机推荐

  1. HIVE几种数据导入方式

    HIVE几种数据导入方式 今天的话题是总结Hive的几种常见的数据导入方式,我总结为四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询 ...

  2. Duang的成长——使用造字程序输入生僻字

    使用造字程序输入生僻字 最近,一个字突然间火了起来,那就是——duang! (图片来自网络) 那么,问题来了!造字程序哪家强?(此处有掌声) 其实,微软早就考虑到各国文字的博大精深,在系统中集成了一个 ...

  3. [原] XAF 如何基于业务规则禁用属性

    How to: Disable Property Editors Based on a Business Rule // Developer Express Code Central Example: ...

  4. python 处理中文文件时的编码问题,尤其是utf-8和gbk

    python代码文件的编码 py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代码 ...

  5. h5专题应该兼容那些浏览器?

    本人做专题还不算很多,但是也很腻烦了.一般一个专题制作也就3天,可是调试得4/5天.除了销售客户各种无休止的改改改.还有一点很重要就是浏览器的兼容性.刚开始做专题的时候天真的以为苹果只要兼容到ipho ...

  6. Dynamic CRM 2013学习笔记(三十五)自定义审批流6 - 审批通过后,再审批 - 二次审批

    最近有个特殊的需求,客户想做二次审批,就是审批通过后,再走一次审批流程.最开始一想,这还不简单,审批通过后,直接把状态改成draft就完了,后来一试,发现一堆问题,比如第一次审批完后,界面是不允许修改 ...

  7. JS备忘录

    /** *删除数组指定下标或指定对象 */ Array.prototype.remove = function (obj) { for (var i = 0; i < this.length; ...

  8. 第十五章:Android 调用WebService(.net平台)

    什么是webservice? Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和 ...

  9. Unity开发游戏 flapybird 无广告老马版分享

    Flapybird确实是一款非常好玩的游戏,但是上手难度比较大.经过老马模仿加工,把游戏难度降低,而且不加入任何广告. 特此分享.下载地址:http://files.cnblogs.com/fly_d ...

  10. JavaScript简洁继承机制实现(不使用prototype和new)

    此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不 ...