Golang | Leetcode Golang题解之第405题数字转换为十六进制数

题目:

题解:

Go 复制代码
func toHex(num int) string {
    if num == 0 {
        return "0"
    }
    sb := &strings.Builder{}
    for i := 7; i >= 0; i-- {
        val := num >> (4 * i) & 0xf
        if val > 0 || sb.Len() > 0 {
            var digit byte
            if val < 10 {
                digit = '0' + byte(val)
            } else {
                digit = 'a' + byte(val-10)
            }
            sb.WriteByte(digit)
        }
    }
    return sb.String()
}
相关推荐
加农炮手Jinx6 小时前
LeetCode 72. Edit Distance 题解
算法·leetcode·力扣
_深海凉_6 小时前
LeetCode热题100-打家劫舍
算法·leetcode·职场和发展
geovindu7 小时前
go: Chain of Responsibility Pattern
开发语言·设计模式·golang·责任链模式
菜鸟丁小真13 小时前
LeetCode hot100 -73.矩阵置零
数据结构·leetcode·矩阵·知识点总结
We་ct13 小时前
LeetCode 64. 最小路径和:动态规划入门实战
开发语言·前端·算法·leetcode·typescript·动态规划
踩坑记录14 小时前
leetcode hot100 169. 多数元素 easy 技巧 摩尔投票
leetcode
水蓝烟雨14 小时前
3487. 删除后的最大子数组元素和
算法·leetcode·链表
菜鸟丁小真16 小时前
LeetCode hot100 -54.螺旋矩阵
算法·leetcode·矩阵·知识点总结
wsoz16 小时前
Leetcode链表-day9
c++·算法·leetcode·链表
6Hzlia17 小时前
【Hot 100 刷题计划】 LeetCode 21. 合并两个有序链表 | C++ 经典迭代与 Dummy 技巧
c++·leetcode·链表