Golang 0ms beats 100%


  • 0
    R
    func toHex(num int) string {
        arr := []byte {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
        
        if num == 0 {
            return "0"
        }
        
        if num < 0 {
            num = -num
            num ^= 0xffffffff
            num += 1
        }
        
        res := ""
        
        for num > 0 {
            digit := num % 16
            res = string(arr[digit]) + res
            num /= 16
        }
        return res
    }
    

Log in to reply
 

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