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 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"
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——学会观察,贪心思路的更多相关文章
- [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 解题报告(Python)
[LeetCode]423. Reconstruct Original Digits from English 解题报告(Python) 标签: LeetCode 题目地址:https://leetc ...
- 【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 ...
随机推荐
- poj 1265 Area (Pick定理+求面积)
链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- Spark ML聚类分析之k-means||
今天更新了电脑上的spark环境,因为上次运行新的流水线的时候,有的一些包在1.6.1中并不支持 只需要更改系统中用户的环境变量即可 然后在eclipse中新建pydev工程,执行环境是python3 ...
- CSS笔记(十)position属性与定位
参考:http://www.w3school.com.cn/cssref/pr_class_position.asp position属性规定了元素的定位类型.任何元素都可定位,其中,绝对或固定元素会 ...
- uploads 上传图片
public static function upFile($r,$p='../images/link/',$type='gif,jpg,png',$named=0){ $newnames = nul ...
- c 字符串常用函数
#include <iostream> #include <stdio.h> #include <vector> #include "string.h&q ...
- ssis freach loop container 传入变量给 某些数据源的时候。
ssis freach loop container 传入变量给 某些数据源的时候. 应该选择loop container ,设置delayvalidateion为true. 这样数据源控件就不会报e ...
- Codeforces 713C Sonya and Problem Wihtout a Legend
题意:给一个序列,可以进行若干次操作,每次操作选择一个数让它+1或-1,问最少要几次操作使得序列变为严格单调递增序列. 题解:首先考虑非严格递增序列,则每个数字最终变成的数字一定是该数组中的某个数.那 ...
- JavaScript基础知识点
本书目录 第一章: JavaScript语言基础 第二章: JavaScript内置对象第三章: 窗口window对象第四章: 文档document对象第五章: 表单form对象第六章: ...
- GO语言中间的derfer
defer Go语言中有种不错的设计,即延迟(defer)语句,你可以在函数中添加多个defer语句.当函数执行到最后时,这些defer语句会按照逆序执行, 最后该函数返回.特别是当你在进行一些打开资 ...
- 转:C的|、||、&、&&、异或、~、!运算
转自:C的|.||.&.&&.异或.~.!运算 位运算 位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位 ...