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)))
  • 相关阅读:
    0034 CSS精灵技术:sprite(重点)
    0033 溢出的文字省略号显示:white-space、overflow、text-overflow
    0032 垂直对齐:vertical-align(图片、表单和文字对齐,去除图片底侧空白缝隙)
    0031 CSS用户界面样式:鼠标样式cursor、轮廓线 outline、防止拖拽文本域resize
    0030 元素的显示与隐藏:dispaly、visibility、overflow
    0029 css定位:相对、绝对、固定、绝对定位盒子居中、z-index、绝对定位改变display属性、案例
    0027 chrome调试工具
    0026 页面布局流程
    0025 CSS属性书写顺序
    0024 Photoshop 切图
  • 原文地址:https://www.cnblogs.com/seyjs/p/10495443.html
Copyright © 2011-2022 走看看