Simple c++ 15 lines accepted solution


  • 0
    I
        string toHex(unsigned int num) {
            if(!num) return "0";
            string res;
            while(num)
            {
                int ms = num & 15;
                if(ms < 10)
                    res.push_back('0' + ms);
                else
                    res.push_back('a' + ms - 10);
                num >>= 4;
            }
            reverse(res.begin(),res.end());
            return res;
        }
    

  • 0
    Y

    but signed num ?


  • 0
    I

    converting to unsigned int avoids infinite loop when n is negative


Log in to reply
 

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