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"
原题是要把字母变成数字。
考查一下0-9的英文,我们可以先找出容易找的数,找完之后再排序
找数的顺序:
- 0 目标z
- 2 目标w
- 4 目标u
- 6 目标x
- 8 目标g
剩下的就只剩下奇数了
- 3 2把t用掉了 目标t
- 5 4把f用掉了 目标f
- 7 5把v用掉了 目标v
剩下one 和 nine
- 1 找o
- 9 找i
class Solution(object):
def originalDigits(self, s):
"""
:type s: str
:rtype: str
"""
d = collections.Counter(s)#用字典做也是一样,会稍微麻烦一点
res = []
for x in '0eroz 2tow 4foru 6six 8eihtg 3reeht 5vief 7eensv 1neo 9nnei'.split():
res.append(x[0] * d[x[-1]])
for c in x:
d[c] -= d[x[-1]]
return ''.join(sorted(res))
本文介绍了一种算法,该算法能够将包含英文数字表述的字符串转换为正确的数字顺序。通过识别特定字母来确定数字的存在,并使用计数器进行排序,确保输出结果的准确性。
504

被折叠的 条评论
为什么被折叠?



