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. python_way day14 CSS

    python_way day14 CSS 层叠样式表 一.CSS作用域: 二.css标签选择器 三.css样式 一.css作用域: 基本用法:style="样式" <body ...

  2. hdu 4712 Hamming Distance 随机

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  3. js中两个对象的比较

    代码取自于underscore.js 1.8.3的isEqual函数. 做了一些小小的修改,主要是Function的比较修改. 自己也加了一些代码解读. <!DOCTYPE html> & ...

  4. git学习笔记08-分支管理策略-实际上我们应该怎么应用分支

    Git用Fast forward模式(快进模式),但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支 ...

  5. Android中的文件权限操作

    默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...

  6. iOS - File Archive/UnArchive 文件压缩/解压

    1.ZipArchive 方式 ZipArchive 只能对 zip 类文件进行压缩和解压缩 GitHub 网址:https://github.com/ZipArchive/ZipArchive Zi ...

  7. Windows_CMD_临时环境变量

    1. 以 path 为例: 1.1.查看: set path 1.2.添加: set path=%path%;"要添加的路径" 附录:下面贴上一些常用的环境变量及作用 %ALLUS ...

  8. Make 教程

    Make 命令教程 原文作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2015/02/make.html (在原文基础上稍作修改) 代码变成可执行文件,叫做编 ...

  9. Android广播BroadcastReceiver 一

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播.当然用户也可以自定义自己的广播. 既然说到广播,那么必定有一个广播发送者,以及广播接收器 ...

  10. 转:Google全程面试题目(未完成)

    转自:http://kb.cnblogs.com/page/95951/ 经过了三个月的断断续续的面试和准备,最近一阵抓了很多时间努力准备, 本以为最后的一次面试能弥补前面的不足,可惜还是功亏一篑.. ...