Golang | Leetcode Golang题解之第482题秘钥格式化

题目:

题解:

Go 复制代码
func licenseKeyFormatting(s string, k int) string {
    ans := []byte{}
    for i, cnt := len(s)-1, 0; i >= 0; i-- {
        if s[i] != '-' {
            ans = append(ans, byte(unicode.ToUpper(rune(s[i]))))
            cnt++
            if cnt%k == 0 {
                ans = append(ans, '-')
            }
        }
    }
    if len(ans) > 0 && ans[len(ans)-1] == '-' {
        ans = ans[:len(ans)-1]
    }
    for i, n := 0, len(ans); i < n/2; i++ {
        ans[i], ans[n-1-i] = ans[n-1-i], ans[i]
    }
    return string(ans)
}
相关推荐
Scabbards_4 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
ValhallaCoder7 小时前
Leetcode-hot100(2026.08.17)
python·算法·leetcode
xcLeigh7 小时前
Go入门:无类型常量与类型常量的区别
服务器·开发语言·golang
泡沫冰@8 小时前
GO 语言基础
开发语言·算法·golang
青 春 记 忆11 小时前
LeetCode 104. 二叉树的最大深度|Python 解法详解
python·算法·leetcode
骇客野人12 小时前
电商商城微服务架构设计方案(SpringCloud/Go+Vue3+MySQL+ES+RocketMQ)
spring cloud·微服务·golang
码农大叔的博客13 小时前
golang示例:for九九乘法表
开发语言·算法·golang
圣殿骑士-Khtangc13 小时前
Go-sync-Pool对象池最佳实践与源码分析
golang
互联网中的一颗神经元14 小时前
01. Go 内存管理全景架构
java·jvm·golang
有脚就行14 小时前
第24篇-Go-gRPC推理服务-高性能跨语言通信
开发语言·人工智能·后端·golang