OJ Lintcode 将整数A转换为B

如果要将整数A转换为B,需要改变多少个bit位?
您在真实的面试中是否遇到过这个题?
Yes
样例
如把31转换为14,需要改变2个bit位。
(31)10=(11111)2
(14)10=(01110)2

class Solution {
public:
    /**
     *@param a, b: Two integer
     *return: An integer
     */
    deque  trantobinary(int a){
        deque vi;
        while(a!=0){
            int rem=a%2;
            int res=a/2;

            vi.push_back(rem);
            a=res;
        }
        return vi;
    }

    deque tobinary(long long num){
        deque d;
        if(num<0){
             num=4294967296+num;
        }

        while(num!=0){
            //余数
            int rem=num%2;
            //商
            int res=num/2;
            num=res;
            d.push_back(rem);
        }
        return d;
    }

    int bitSwapRequired(int a, int b) {
        // write your code here
        int counts=0;
        deque va=tobinary(a);
        deque vb=tobinary(b);

        int diff=va.size()-vb.size();
        diff=abs(diff);
        if((va.size())<(vb.size())){
            for(int i=0;i

你可能感兴趣的:(OJ Lintcode 将整数A转换为B)