bitmapping

268. Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

自己的解法: 排序,相邻相差不是1的位置是丢失的数

class Solution {
public:
    int missingNumber(vector<int>& nums) {
        int n=nums.size();
        sort(nums.begin(),nums.end());
        if(nums[0]==1) return 0;
        for(int i=0;i<nums.size()-1;i++)
        {
            if(nums[i+1]-nums[i]!=1) return nums[i+1]-1;
        }
        return n;
    }
};
使用位运算,用抑或,出现两次的数会是0,最后与size()抑或可得丢失的数



338. Counting Bits


Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:

  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

这道题主要的思路是使用之前的结果:

有几种公式:

f[i] = f[i >> 1] + (i & 1)

ret[i] = ret[i&(i-1)] + 1;

class Solution {
public:
    vector<int> countBits(int num) {
       
        vector<int> ret(num+1,0);
        for(int i=1;i<=num;i++)
        {
            ret[i]=ret[i&(i-1)]+1;
        }
        return ret;
    }
};
461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ xy < 231.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1   (0 0 0 1)
4   (0 1 0 0)
       ?   ?

The above arrows point to positions where the corresponding bits are different.
class Solution {
public:
    int hammingDistance(int x, int y) {
        int res=0;
        while(x||y)
        {
            int temp1=x&1;
            int temp2=y&1;
            if(temp1!=temp2)
                res++;
            x>>=1;
            y>>=1;
        }
        return res;
    }
};



476. Number Complement

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 
求一个数的补

class Solution {
public:
    int findComplement(int num) {
        int res=0;
        int i=0;
        while(num)
        {
            int temp=num&1;
            temp=temp?0:1;
            res+=temp<<i;
            i++;     
            num>>=1;
        }
        return res;
    }
};
其他好的方法:

class Solution {
public:
    int findComplement(int num) {
        unsigned mask = ~0;
        while (num & mask) mask <<= 1;
        return ~mask & ~num;
    }
};




371. Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

Example:
Given a = 1 and b = 2, return 3.

不使用加减号做加法

class Solution {
public:
    int getSum(int a, int b) {
        //两个数相加,一0一1的位置成为1,都是1的位置需要进位
        return b==0?a:getSum(a^b,(a&b)<<1);
    }
};

405. Convert a Number to Hexadecimal

Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.

Note:

  1. All letters in hexadecimal (a-f) must be in lowercase.
  2. The hexadecimal string must not contain extra leading 0s. If the number is zero, it is represented by a single zero character '0'; otherwise, the first character in the hexadecimal string will not be the zero character.
  3. The given number is guaranteed to fit within the range of a 32-bit signed integer.
  4. You must not use any method provided by the library which converts/formats the number to hex directly.

Example 1:

Input:
26

Output:
"1a"

Example 2:

Input:
-1

Output:
"ffffffff"




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值