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

傷城~ 2022-08-07 10:55 30阅读 0赞

一: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)

  1. class Solution {
  2. public:
  3. int hammingWeight(uint32_t n) {
  4. int count = 0;
  5. for(int i = 0; i < 32; i++){
  6. if((n & (1<<i)) != 0)count++;
  7. }
  8. return count;
  9. }
  10. };

解法二:此解关键在于明白n&(n-1)会n最后一位1消除,这样循环下去就可以求出n的位数中为1的个数

  1. class Solution {
  2. public:
  3. int hammingWeight(uint32_t n) {
  4. int count = 0;
  5. while(n > 0){
  6. n &= n-1;
  7. count ++;
  8. }
  9. return count;
  10. }
  11. };

二: 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(这样就减免了很多不必要按位与的过程)

解法一:

  1. class Solution {
  2. public:
  3. int rangeBitwiseAnd(int m, int n) {
  4. int bitm = 0, bitn = 0;
  5. for(int i =0; i < 31; i++){
  6. if(m & (1<<i))bitm = i;
  7. if(n & (1<<i))bitn = i;
  8. }
  9. if(bitm == bitn){
  10. int sum = m;
  11. for(int i = m; i < n; i++) // 为了防止 2147483647+1 超过范围
  12. sum = (sum & i);
  13. sum = (sum & n);
  14. return sum;
  15. }
  16. else return 0;
  17. }
  18. };

解法二:

  1. class Solution {
  2. public:
  3. int rangeBitwiseAnd(int m, int n) {
  4. while(n > m){
  5. n &= n-1;
  6. }
  7. return n;
  8. }
  9. };

三: 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

  1. class Solution {
  2. public:
  3. bool isHappy(int n) {
  4. while(n != 1){
  5. if(hset.count(n)) return false; // 通过hashtable 判断是否出现过
  6. hset.insert(n);
  7. int sum = 0;
  8. while(n != 0){ // 求元素的各个位置平方和
  9. int mod = n%10;
  10. n = n/10;
  11. sum += mod * mod;
  12. }
  13. n = sum;
  14. }
  15. return true;
  16. }
  17. private:
  18. set<int> hset;
  19. };

四: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?

分析:此题关键在于用到异或

  1. class Solution {
  2. public:
  3. int singleNumber(vector<int>& nums) {
  4. int ans = 0;
  5. for(int i = 0; i < nums.size(); i++)
  6. ans ^= nums[i];
  7. return ans;
  8. }
  9. };

发表评论

表情:
评论列表 (有 0 条评论,30人围观)

还没有评论,来说两句吧...

相关阅读