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

标签: LeetCode


题目地址:https://leetcode.com/problems/reconstruct-original-digits-from-english/description/

题目描述:

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把所有能够成的都提前构成,最后就是结果了。这样不对,因为可能会有剩余的字符了。这个题说了,不会有剩余字符。

这个题不是很好,并没有考编程的思想,而是考的找规律。。面试应该不会问的。

错误代码:

class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
number = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
count = collections.Counter(s)
res = ''
for i, num in enumerate(number):
while True:
word_count = 0
for c in num:
if count[c] > 0:
word_count += 1
if word_count == len(num):
res += str(i)
count.subtract(collections.Counter(num))
else:
break
return res

正确做法如下:

选自:http://bookshadow.com/weblog/2016/10/16/leetcode-reconstruct-original-digits-from-english/

统计字符串s中各字符的个数,需要注意的是,在枚举英文字母时,需要按照特定的顺序方可得到正确答案。

例如按照顺序:6028745913,这个顺序可以类比拓扑排序的过程。

观察英文单词,six, zero, two, eight, seven, four中分别包含唯一字母x, z, w, g, v,
u;因此6, 0, 2, 8, 7, 4需要排在其余数字之前。

排除这6个数字之后,剩下的4个数字中,按照字母唯一的原则顺次挑选。

由于剩下的单词中,只有five包含f,因此选为下一个单词;

以此类推,可以得到上面所述的顺序。

在上文代码的基础上,我进行了一点点的技巧性的改变,就是Counter()自带的有subtract()方法,直接从字典中减去了新字典。。

class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
cnts = collections.Counter(s)
nums = ['six', 'zero', 'two', 'eight', 'seven', 'four', 'five', 'nine', 'one', 'three']
numc = [collections.Counter(num) for num in nums]
digits = [6, 0, 2, 8, 7, 4, 5, 9, 1, 3]
ans = [0] * 10
for idx, num in enumerate(nums):
cntn = numc[idx]
t = min(cnts[c] / cntn[c] for c in cntn)
ans[digits[idx]] = t
for i in range(t):
cnts.subtract(cntn)
return ''.join(str(i) * n for i, n in enumerate(ans))

日期

2018 年 3 月 13 日

【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)的更多相关文章

  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——学会观察,贪心思路

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

  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. ansible-playbook 编译安装nginx

    mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv └── nginx ├── ...

  2. C#点击按钮添加标签

    <asp:Button ID="button1" runat="server" Text="创建" onclick="But ...

  3. LeetCode两数之和

    LeetCode 两数之和 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是 ...

  4. 日常Java 2021/10/31

    泛型类 泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分.和迈型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开.一个泛型参数,也被称为一个类型变量, ...

  5. Linux—禁止用户SSH登录方法总结

    Linux-禁止用户SSH登录方法总结 一.禁止用户登录 1.修改用户配置文件/etc/shadow       将第二栏设置为"*",如下.那么该用户就无法登录.但是使用这种方式 ...

  6. Dubbo服务分组

    服务分组与多版本控制的使用方式几乎是相同的,只要将version替换为group即可.但使用目的不同.使用版本控制的目的是为了升级,将原有老版本替换掉,将来不再提供老版本的服务,所以不同版本间不能出现 ...

  7. springboot优雅实现异常处理

    前言 在平时的 API 开发过程中,总会遇到一些错误异常没有捕捉到的情况.那有的小伙伴可能会想,这还不简单么,我在 API 最外层加一个 try...catch 不就完事了. 哈哈哈,没错.这种方法简 ...

  8. Zookeeper客户端链接

    一.zkCli.sh ./zkCli.sh -server 39.97.176.160:2182 39.97.176.160 : zookeeper服务器Ip 2182:zookeeper端口 二.Z ...

  9. js调用高德地图API获取地理信息进行定位

    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=(需要自 ...

  10. 【spring AOP】AspectJProxyFactory

    AspectJProxyFactory,可能大家对这个比较陌生,但是@Aspect这个注解大家应该很熟悉吧,通过这个注解在spring环境中实现aop特别的方便. 而AspectJProxyFacto ...