zoukankan      html  css  js  c++  java
  • 【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"

    解题思路:观察0~9所有的英文表达,可以发现有一些字符只会出现一次,例如z只有在zero中有,w在two中有,整理可以得到如下。

           #phase 1
            uniq_dic_1 = {}
            uniq_dic_1['z'] = ('zero','0')
            uniq_dic_1['w'] = ('two','2')
            uniq_dic_1['u'] = ('four','4')
            uniq_dic_1['x'] = ('six','6')
            uniq_dic_1['g'] = ('eight','8')

    除去这五个单词后,继续寻找只有唯一与众不同字符的单词,得到如下。

            #phase 2
            uniq_dic_2 = {}
            uniq_dic_2['o'] = ('one','1')
            uniq_dic_2['t'] = ('three', '3')
            uniq_dic_2['f'] = ('five', '5')
            uniq_dic_2['s'] = ('seven', '7')

    除去上面找到的9个,最后就只剩下nine了。

            #phase 3
            uniq_dic_3 = {}
            uniq_dic_3['i'] = ('nine', '9')

    解题的方法,是先去 phase 1 中找出唯一字符在s中出现了几次,出现了几次就表示对应的单词出现了几次,扣除掉这个单词其余字符出现的次数;接下来是phase 2和phase 3,即可得到所有单词出现的次数。

    代码如下:

    class Solution(object):
        def calc(self,dic_src,uniq_dic):
            r = ''
            for key in uniq_dic.iterkeys():
                if key in dic_src:
                    count = dic_src[key]
                    r += uniq_dic[key][1] * count
                    for char in uniq_dic[key][0]:
                        dic_src[char] -= count
                        if dic_src[char] == 0:
                            del dic_src[char]
            return r
    
        def originalDigits(self, s):
            """
            :type s: str
            :rtype: str
            """
            dic_src = {}
            for i in s:
                dic_src[i] = dic_src.setdefault(i, 0) + 1
    
            #phase 1
            uniq_dic_1 = {}
            uniq_dic_1['z'] = ('zero','0')
            uniq_dic_1['w'] = ('two','2')
            uniq_dic_1['u'] = ('four','4')
            uniq_dic_1['x'] = ('six','6')
            uniq_dic_1['g'] = ('eight','8')
    
            #phase 2
            uniq_dic_2 = {}
            uniq_dic_2['o'] = ('one','1')
            uniq_dic_2['t'] = ('three', '3')
            uniq_dic_2['f'] = ('five', '5')
            uniq_dic_2['s'] = ('seven', '7')
    
            #phase 3
            uniq_dic_3 = {}
            uniq_dic_3['i'] = ('nine', '9')
            res = ''
            res += self.calc(dic_src, uniq_dic_1)
            res += self.calc(dic_src, uniq_dic_2)
            res += self.calc(dic_src, uniq_dic_3)
    
    
            return ''.join(sorted(list(res)))
  • 相关阅读:
    ThinkPHP中的__URL__或__URL__等
    如何输出其他模块的操作模板
    如何改变ThinkPHP默认访问模块
    C#中文件操作
    常见的网页技巧(转)
    JS实现验证码局部更新
    winform控件随窗体大小变化而变化
    php 观察者模式
    PHP生命周期
    Mac 安装redis
  • 原文地址:https://www.cnblogs.com/seyjs/p/10495443.html
Copyright © 2011-2022 走看看