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)
}
相关推荐
py有趣4 小时前
力扣热门100题之接雨水
算法·leetcode
不会写DN5 小时前
构建一个抗揍的 Go TCP 聊天服务:异常兜底与防御性编程实践
tcp/ip·golang·php
不会写DN7 小时前
Go中如何跨语言实现传输? - GRPC
开发语言·qt·golang
py有趣8 小时前
力扣热门100题之合并区间
算法·leetcode
派大星~课堂8 小时前
【力扣-138. 随机链表的复制 ✨】Python笔记
python·leetcode·链表
cpp_25018 小时前
P10108 [GESP202312 六级] 闯关游戏
数据结构·c++·算法·动态规划·题解·洛谷·gesp六级
py有趣8 小时前
力扣热门100题之最小覆盖子串
算法·leetcode
北顾笙9809 小时前
day15-数据结构力扣
数据结构·算法·leetcode
人道领域10 小时前
【LeetCode刷题日记:24】两两交换链表
算法·leetcode·链表
北顾笙98010 小时前
day16-数据结构力扣
数据结构·算法·leetcode