Simple Java solution with comment


  • 77
    /*
    Basic idea: each time we take a look at the last four digits of
                binary verion of the input, and maps that to a hex char
                shift the input to the right by 4 bits, do it again
                until input becomes 0.
    
    */
    
    public class Solution {
        
        char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        
        public String toHex(int num) {
            if(num == 0) return "0";
            String result = "";
            while(num != 0){
                result = map[(num & 15)] + result; 
                num = (num >>> 4);
            }
            return result;
        }
        
        
    }````

  • 13
    T

    My corresponding C++ code. I must add result.size()<8 in the while condition, or it will loop forever with a negative input.

    class Solution {
    public:
        string toHex(int num) {
            if (num == 0) return "0";
            vector<char> toHexChar = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            string result;
            while (num != 0 && result.size()<8){
                result = toHexChar[num&15] + result;
                num = num >> 4;
            }
            return result;
        }
    };
    

  • 9
    B

    StringBuilder may be used here.

        public String toHex(int num) {
            StringBuilder sb = new StringBuilder();
            do {
                int n = num & 0xf;
                n += n < 0xa ? '0' : 'a' - 10;
                sb.append((char)n);
            } while ((num >>>= 4) != 0); 
            return sb.reverse().toString();
        }
    

  • 0
    I

    char[] may be used here.

            char[] ans = new char[8];
            int offset = 8;
            do {
                int n = num & 0xf;
                n += n > 9 ? 'a' - 10 : '0';
                ans[--offset] = (char)n;
                num >>>= 4;
            } while (num != 0);
            return new String(ans, offset, 8 - offset);
    

  • 1
    D
                result = map[(num & 0x0F)] + result; 
    

    15 replace 0x0F


  • 2
    F

    @truffleyang

    Your solution is right. However a "more correct" way to do unsgined right shift, IMO, is using an unsigned value, in which way the .size() doesn't needed to be checked and the code is similar to that in Java.

    (signed right shift is undefined.)

    class Solution {
    public:
        string toHex(int num) {
            if (num == 0) return "0";
            unsigned int n = static_cast<unsigned int>(num);
            
            vector<char> map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            string result;
            
            while (n != 0) {
                result.push_back(map[n & 0b1111]);
                n >>= 4;
            }
            
            std::reverse(result.begin(), result.end());
            return result;
            
        }
    };
    

  • 8
    S

    I like your user name


  • 7
    Y

    Make programmers great again?!
    Anyway, nice and clear answer.


  • 8

    Thanks Mr. Trump.


  • 1
    C

    Awesome Solution, Thx a lot!!!


  • 1
    G

    Thank you for making computer science great again


  • 8

    @DonaldTrump I hope you become President Mr Trump. This solution is great, even "tremendous". I believe you will bring similarly tremendous solutions to our country currently governed by the "corrupt and stupid" politicians.


  • 4
    Z

    Donald Trump, I hope that you can offer us more H1B friendly policies just like this answer. I would vote for you.


  • 0

    Thanks President Trump!


  • 0
    I

    @truffleyang FYI no need to check size everytime. You can do a logical shift in c++ by casting num to unsigned int.

       while (num != 0) {
            result = toHexChar[num&15] + result;
            num = (unsigned int) num >> 4;
        }

  • 0
    S

    @adam47 said in Simple Java solution with comment:

    Thanks Mr. Trump.

    Shocked by OP's name.


  • 2

    @s I approve this message. #MAGA


  • 0

    I think we should take negative number into consideration, add the code as following before while loop.
    if num < 0:
    num = -num
    num ^= 0xffffffff
    num += 1


  • 0

    @木的3次方
    The solution has already taken negative input into consideration by using the unsigned shift ">>>" as opposed to ">>"


  • 1
    M

    How to come up with using 15 to do the & operation with the num?
    I know how it works but don't know how it is obtained.
    Can someone help clarify that?
    Thx,


Log in to reply
 

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