Java Solution Beats 86%


  • 0
    public class Solution {
        private static final char[] digits = "0123456789abcdef".toCharArray();
    	public String toHex(int num) {
    		StringBuilder result = new StringBuilder();
    		int i = 28;
    		while (i > 0 && ((num & (0xf << i)) >>> i) == 0)
    			i -= 4;
    		for (; i >= 0; i -= 4) 
    			result.append(digits[(num & (0xf << i)) >>> i]);
    		return result.toString();
    	}
    }
    

Log in to reply
 

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