leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number

一:Number of 1 Bitsgit

題目:app

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).less

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.oop

Credits:
Special thanks to @ts for adding this problem and creating all test cases.ui

解法一:

此題關鍵是怎樣推斷一個數字的第i爲是否爲0  即: x& (1<<i)this

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的個數spa

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

題目:code

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

For example, given the range [5, 7], you should return 4.ip

分析:此題提供兩種解法: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;
        
    }
};
相關文章
相關標籤/搜索