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)
}
相关推荐
客卿1232 小时前
力扣100-移动0
算法·leetcode·职场和发展
緈福的街口8 小时前
【leetcode】347. 前k个高频元素
算法·leetcode·职场和发展
roman_日积跬步-终至千里9 小时前
【Go语言基础【3】】变量、常量、值类型与引用类型
开发语言·算法·golang
roman_日积跬步-终至千里9 小时前
【Go语言基础】基本语法
开发语言·golang·xcode
小河豚oO13 小时前
LeetCode刷题---贪心算法---944
算法·leetcode·贪心算法
Once_day13 小时前
代码训练LeetCode(23)随机访问元素
算法·leetcode
小河豚oO14 小时前
LeetCode 热题 100 - 哈希 - 128
算法·leetcode·哈希算法
客卿12314 小时前
力扣100题之128. 最长连续序列
算法·leetcode·哈希算法
T1an-114 小时前
【力扣链表篇】206.反转链表
算法·leetcode·链表
Once_day15 小时前
代码训练LeetCode(24)数组乘积
算法·leetcode