【LeetCode】423. Reconstruct Original Digits from English 解题报告(Python)
【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:
- Input contains only lowercase English letters.
- 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.
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)的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 423 Reconstruct Original Digits from English 从英文中重建数字
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字.注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "ab ...
- 423. Reconstruct Original Digits from English
这个题做得突出一个蠢字.. 思路就是看unique letter,因为题里说肯定是valid string.. 一开始有几个Z就有几个ZERO 同样的还有x for six, g for eight, ...
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- Leetcode: Reconstruct Original Digits from English
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
随机推荐
- CentOS7忘记root密码如何重置
1.重启服务器 2.修改启动文件 3.修改密码 4.重启,测试 ① 重启服务器,按"e"键进入修改系统开机项界面 ② 修改启动文件 "ro" -> ...
- 从零开始学习oracle
引用博客:https://blog.csdn.net/qq_36998053/article/details/82725765 )Oracle之<环境配置> (二)Oracle之<基 ...
- Spark3学习入门【基于Java】
Spark 是离线数据处理的一种大数据技术,和Flick相比数据处理要延后,因为Flick是实时数据处理,而Spark需要先读取数据到内存. Spark的库是基于Scala写的,虽然Scala也是运行 ...
- python基础实战
字符串的互相转换 字典的排序 字典的排序可以直接把,key值或者,values拿出来排序 也可以用dict.items拿出所有的key,value的值再加key=lambda x:x[1] 来排序. ...
- 学习java 7.9
学习内容: Date类 Date类常用方法 SimpleDateFormat 1.格式化(从Date到String) public final String format(Date date) 将日期 ...
- javaIO——输入输出流
字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...
- LoadRunner中怎么设置密码参数化与用户名关联
对密码参数化时从parameter里的"Select next row"列表中选择Same Line As这一选项,意思就是每一个密码参数化取值与对应行的用户名关联起来了
- spring整合mybatis和springmvc的配置概图
- 【C/C++】从矩阵左上角走到右下角
tx的笔试,但是只过了10%,就离谱 #include <bits/stdc++.h> using namespace std; const int maxn = 1010; long d ...
- 访问Github速度很慢以及解决方法(系统通用)
原因分析1,CDN,Content Distribute Network,可以直译成内容分发网络,CDN解决的是如何将数据快速可靠从源站传递到用户的问题.用户获取数据时,不需要直接从源站获取,通过CD ...