straightforward c++ solution 0ms


  • 0
    M
    class Solution {
    public:
        string helper(unsigned int num)
        {
          string res;
          while (num)
          {
            int remain = num % 16;
            res.push_back(remain < 10 ? remain + '0' : 'a' + remain - 10);
            num /= 16;
          }
          reverse(res.begin(), res.end());
          return res.empty() ? "0" : res;        
        }
    
        string toHex(int num) {
          return helper(num);
        }
    };
    

Log in to reply
 

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