本文主要是介绍LeetCode: Reconstruct Original Digits from English,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这道 LeetCode 上的题目,还是有点难度,有点意思的。就是要把给定的字符串中的英文,组合成 0,1,2,3,4,5,6,7,8,9 这几个数字的英文,再按升序将数字输出。
具体的,题目描述如下:
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.
参考 discuss 部分,python 实现如下:
import collectionsdef originalDigits(self, s):""":type s: str:rtype: str"""dic = collections.Counter(s)cnt = [0] * 10cnt[0] = dic['z']for d in 'zero': dic[d] = dic[d] - cnt[0]cnt[2] = dic['w']for d in 'two': dic[d] = dic[d] - cnt[2]cnt[4] = dic['u']for d in 'four': dic[d] = dic[d] - cnt[4]cnt[6] = dic['x']for d in 'six': dic[d] = dic[d] - cnt[6]cnt[8] = dic['g']for d in 'eight': dic[d] = dic[d] - cnt[8]cnt[1] = dic['o']for d in 'one': dic[d] = dic[d] - cnt[1]cnt[3] = dic['r']for d in 'three': dic[d] = dic[d] - cnt[3]cnt[5] = dic['f']for d in 'five': dic[d] = dic[d] - cnt[5]cnt[7] = dic['v']for d in 'seven': dic[d] = dic[d] - cnt[7]cnt[9] = dic['i']for d in 'nine': dic[d] = dic[d] - cnt[9]output_s = ''for i, c in enumerate(cnt):output_s = output_s + str(i) * creturn output_s
提交结果如下:
参考链接:
1. https://discuss.leetcode.com/topic/63389/straightforward-python-solution-o-n
2. https://discuss.leetcode.com/topic/63396/naive-python-solution
这篇关于LeetCode: Reconstruct Original Digits from English的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!