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"
class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
six, zero, two, eight, four中分别包含唯一字母x, z, w, g, u, 根据z,w,x,g,u的个数就可以知道0,2,6,8,4的个数。对于剩下的one,three,five,seven,one可以由字符o的个数减去在0,2,4,6,8中出现的o的个数。
"""
char_cnt = [0]*26
for c in s:
char_cnt[ord(c)-ord('a')] += 1
mapp_digits = {"zero": 0, "one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9}
digits_c = {"zero": "z", "two": "w", "four": "u", "six": "x", "eight": "g"}
digits_c2 = {"one": "o", "three": "h", "five": "f", "seven": "s"}
digits_c3 = {"nine": "i"}
out = []
for digits in (digits_c, digits_c2, digits_c3):
for d in digits:
cnt = char_cnt[ord(digits[d])-ord('a')]
if cnt > 0:
out += [mapp_digits[d]] * cnt
for c in d:
char_cnt[ord(c)-ord('a')] -= cnt
out.sort()
return "".join(str(c) for c in out)

LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路的更多相关文章

  1. [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 ...

  2. 【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)

    [LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...

  3. 【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 ...

  4. 423. Reconstruct Original Digits from English (leetcode)

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

  5. 423. Reconstruct Original Digits from English(Medium)

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

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

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

  7. 423. Reconstruct Original Digits from English

    这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...

  8. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

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

  9. Leetcode: Reconstruct Original Digits from English

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

随机推荐

  1. hdu 栈题1022&1702

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 http://blog.csdn.net/swm8023/article/details/6902426此 ...

  2. bzoj 1208: [HNOI2004]宠物收养所 set

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7328  Solved: 2892[Submit][Sta ...

  3. hd1496---->这道题是水水的数论吗?

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1496 题意: Consider equations having the following form: ...

  4. XAF实现运行时填加验证规则并保存到数据库中

    有几种方法可以用来声明一个验证规则.最常用的方法是使用对应的Attribute来定义.详见这里.验证模块还允许您通过在业务类实现 IRuleSource 接口定义自定义的验证规则的来源. IRuleS ...

  5. jsp get方式请求参数中包含中文乱码问题解决

    1. 自己接收到参数之后在后台进行转码处理 2: 修改tomcat的配置文件  server.xml <Connector port="8080" protocol=&quo ...

  6. 表单美化-原生javascript和jQuery下拉列表(兼容IE6)

    效果: 思想:用其他标签配合脚本和隐藏的input并通过传值模拟表单元素中的select <!DOCTYPE HTML> <html lang="en-US"&g ...

  7. RAR暴破

    1. 网上稍微搜索了一下,貌似一个叫 "ARPR"的软件 出现的频率较高. 2. http://jingyan.baidu.com/article/a948d651b954a90a ...

  8. java的重修之路

    一.内存管理 java里的声明分引用与基本数据类型. 数组: java里new一个对象数组为  person[] A; A = new person[4];  person[0] = new pers ...

  9. Python学习笔记2—内置函数

    函数的使用 官方文档:https://docs.python.org/2/library/functions.html

  10. 在Spark上用Scala实验梯度下降算法

    首先参考的是这篇文章:http://blog.csdn.net/sadfasdgaaaasdfa/article/details/45970185 但是其中的函数太老了.所以要改.另外出发点是我自己的 ...