Share my recursion solution in C++


  • 0
    O

    class Solution {
    public:

    char help(int n) {
        assert(n >= 0 && n <= 15);
        if (n <= 9) return '0' + n;
        return 'a' + (n - 10);
    }
    
    string transform(unsigned n) {
        string ans = n >= 16 ? transform(n / 16) : "";
        ans.push_back(help(n % 16));
        return ans;
    }
    
    string toHex(int num) {
        unsigned input = num;
        return transform(input);
    }
    

    };


Log in to reply
 

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