C++ 0ms using bitwise


  • 0
    V
    class Solution {
    public:
        string toHex(int num) {
            if(num==0) return "0";
            string rs;
            char hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            rs+=hex[(num&4026531840)>>28];
            rs+=hex[(num&251658240)>>24];
            rs+=hex[(num&15728640)>>20];
            rs+=hex[(num&983040)>>16];
            rs+=hex[(num&61440)>>12];
            rs+=hex[(num&3840)>>8];
            rs+=hex[(num&240)>>4];
            rs+=hex[num&15];
            for(int i=0;i<8;i++){
                if(rs[i]!='0'){
                    rs=rs.substr(i,8);
                    break;
                }
            }
            return rs;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.