Golang | Leetcode Golang题解之第423题从英文中重建数字

题目:

题解:

Go 复制代码
func originalDigits(s string) string {
    c := map[rune]int{}
    for _, ch := range s {
        c[ch]++
    }

    cnt := [10]int{}
    cnt[0] = c['z']
    cnt[2] = c['w']
    cnt[4] = c['u']
    cnt[6] = c['x']
    cnt[8] = c['g']

    cnt[3] = c['h'] - cnt[8]
    cnt[5] = c['f'] - cnt[4]
    cnt[7] = c['s'] - cnt[6]

    cnt[1] = c['o'] - cnt[0] - cnt[2] - cnt[4]

    cnt[9] = c['i'] - cnt[5] - cnt[6] - cnt[8]

    ans := []byte{}
    for i, c := range cnt {
        ans = append(ans, bytes.Repeat([]byte{byte('0' + i)}, c)...)
    }
    return string(ans)
}
相关推荐
microrain2 小时前
同一个寄存器,四种读法:SagooIoT Modbus 驱动的字节序、宽度与功能码
物联网·golang·开源·sagooiot
JWASX4 小时前
Java 转 go 学习 - 并发编程(2)
学习·golang
All for pursuit.4 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Navigator_Z4 小时前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode
ttwuai6 小时前
Go开源后台管理系统推荐:README、源码和License应该怎么验
开发语言·golang·开源
JWASX6 小时前
Java 转 go 学习 - 并发编程(1)
学习·golang
旖旎夜光7 小时前
LeetCode 1576: 替换所有的问号(模拟) —— 题解
c++·学习·算法·leetcode·力控
longlongzihan7 小时前
回溯法详解:LeetCode 46. 全排列
算法·leetcode·回溯
JWASX7 小时前
Java 转 go 学习 - 接口
学习·golang
小小龙学IT7 小时前
Go 语言 reflect 反射包深度解析:从 Type/Value 到三大定律与工业实践
前端·golang