Tuesday, August 29, 2017

405. Convert a Number to Hexadecimal

https://leetcode.com/problems/convert-a-number-to-hexadecimal/description/

Solution 2.
    string toHex(int num) {
        if(num==0) return "0";
        vector<char> dict {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        string res;
        for(int i=0;i<8 && num;i++){
            res = dict[num & 15] + res;
            num >>= 4;
        }
        return res;
    }

No comments:

Post a Comment