题目如下:

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 1uniq_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 2uniq_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 3uniq_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] * countfor char in uniq_dic[key][0]:dic_src[char] -= countif dic_src[char] == 0:del dic_src[char]return rdef originalDigits(self, s):""":type s: str:rtype: str"""dic_src = {}for i in s:dic_src[i] = dic_src.setdefault(i, 0) + 1#phase 1uniq_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 2uniq_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 3uniq_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)))

转载于:https://www.cnblogs.com/seyjs/p/10495443.html

【leetcode】423. Reconstruct Original Digits from English相关推荐

  1. leetcode 423. Reconstruct Original Digits from English | 423. 从英文中重建数字(Java)

    题目 https://leetcode.com/problems/reconstruct-original-digits-from-english/ 题解 class Solution {public ...

  2. 【leetcode】332. Reconstruct Itinerary

    题目如下: Given a list of airline tickets represented by pairs of departure and arrival airports [from, ...

  3. LeetCode Reconstruct Original Digits from English

    题意:给出一个字符串,其是由0-9数字的英文打散后的,求其原始的数字 思路:刚开始用map来做,依次遍历zero,one,这样会有问题,可能有剩余的字符串不能构成数字的英文表示. 依赖唯一的字符数字表 ...

  4. Reconstruct Original Digits from English 从英文中重建数字

    给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字. 注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "abc&quo ...

  5. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  6. 【LeetCode】【HOT】17. 电话号码的字母组合(递归)

    [LeetCode][HOT]17. 电话号码的字母组合 文章目录 [LeetCode][HOT]17. 电话号码的字母组合 package hot;import java.util.ArrayLis ...

  7. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  8. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  9. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

最新文章

  1. 【POJ】3268 Silver Cow Party (将有向图的边反转)
  2. 功能实现了软件就做好了吗?
  3. html全局布局 vue_基于Vue+Element的电商后台管理系统
  4. mysql 锁 会话_MySQL会话锁等待关系原理
  5. 让Vim更好用 for Mac OS X | Hessian's Blog
  6. 最新可用双端美化版在线音乐播放器网站源码
  7. Matlab生成zipf随机数
  8. JavaScript|表格背景颜色改变页面
  9. mysql特殊字符转义_MYSQL特殊字符转义
  10. 【浏览器】【vue】修改网页上的页签图标
  11. androrid按键事件
  12. 论文研读-社交媒体可视化-大规模地理社交媒体数据的可视化抽象与探索
  13. 湖南省第十届蓝狐网络杯大学生计算机程序设计竞赛,2019年湖南省大学生计算机程序设计竞赛 (HNCPC2019) 简要题解...
  14. Ruby 文件目录操作(实例讲解更易懂)
  15. 炒菜模型——各种编程范式
  16. Xshell的安装与使用
  17. python之循环语句
  18. 临河三中宏志班2021年高考成绩查询,临河三中名教师简介一
  19. 有穷自动机(Finite Automate)及其分类和转化
  20. gm修改爆率需要重启服务器吗,传奇私服GM如何调试爆率、刷怪等

热门文章

  1. Python pip install 包后,pycharm仍然提示No module named(提示 No module named ‘pip‘)
  2. Docker 的4种网络模式
  3. CentOS7 下安装 Redis
  4. WebDriver高级应用实例(3)
  5. python数据类型及字符编码
  6. Spring集成Memcached三种方式(一)
  7. Linux shell - 重命名文件和文件夹(mv)
  8. 使用visual studio 2013读取.mat文件
  9. WebStorm 9 配置 Live Edit 功能与浏览器实现同步
  10. iOS字符串常用用法