Leetcode 423. Reconstruct Original Digits from English


Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

Note:

  1. Input contains only lowercase English letters.
  2. Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.
  3. Input length is less than 50,000.

Example 1:

Input: "owoztneoer"

Output: "012"

Example 2:

Input: "fviefuro"

Output: "45"


思路描述:

zero  one two three four five six seven eight nine

0         1     2      3       4      5     6      7      8        9

分析数字的字母构成,如果字符串含有 z,  则根据题目条件字符串总是有效的来看,必有一个  zero的组合在原串中,那么取出一个zero, 记录0的出现次数加1

再次循环,直至没有z为止。

 出现w,必有two

 出现u,必有four

 出现x,必有six

 出现g,必有 eight


以上完成后,剩下的数字仅仅剩下 one,  five, seven, three, nine

此时只需要在剩下的找标志性支付,f : five,  s : seven, h: three o : one


最后一轮,只剩nine,查找结束。以下为代码实现。



import "strings"

import "fmt"
func originalDigits(s string) string {
    s = strings.ToLower(s)
    var result0 [10]int
    for i := 0; i < 10; i++ {
        result0[i] = 0;
    }
    result := result0[:]
    var letterMap = make(map[rune]int)
    for _, c := range  s {
        _, ok := letterMap[c]
        if !ok {
            letterMap[c] = 1
        } else {
            count := letterMap[c]
            letterMap[c] = count + 1
        }
    }
   var numberMap = map[int][]rune {
        0 : []rune {'z','e','r','o'},
        1 : []rune {'o','n','e'},
        2 : []rune {'t','w','o'},
        3 : []rune {'t','h','r','e','e'},
        4 : []rune {'f','o','u','r'},
        5 : []rune {'f','i','v','e'},
        6 : []rune {'s','i','x'},
        7 : []rune {'s','e','v','e','n'},
        8 : []rune {'e','i','g','h','t'},
        9 : []rune {'n','i','n','e'},
    }
    
    
    // find w, h,u, x, g, z
    var para = map[rune]int {
        'z': 0,
        'w': 2,
        'u': 4,
        'x': 6,
        'g': 8,
    }
    
    genData(para,numberMap,letterMap,result)
    
    // find o,f,s
    var para1 = map[rune] int {
        'o': 1,
        'f': 5,
        's': 7,
        'h': 3,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值