Python:每日一题 384
今天的题目:给定一个非空字符串,其中包含字母顺序打乱的表示数字 0-9的英文单词。按升序输出原始的数字。
注意:1. 输入只包含小写英文字母。
2. 输入保证合法并可以转换为原始的数字,例如不会出现类似于 "abc" 或 "zerone" 的输入。
示例 1:
输入:"owoztneoer"
输出:"012" (zeroonetwo)
示例 2:
输入:"fviefuro"
输出:"45" (fourfive)
{:10_298:}欢迎大家一起答题!{:10_298:} 本帖最后由 永恒的蓝色梦想 于 2020-4-26 14:16 编辑
直接看题解了class Solution:
def originalDigits(self, s: str) -> str:
map=collections.Counter(s);
map=,map['o'],map['w'],map['t'],map['r'],map['v'],map['x'],map['s'],map['g'],map['i']]
map-=map+map
map-=map+map
map-=map
map-=map+map+map
map-=map
map-=map+map+map
return ''.join(i.__str__()*j for i,j in enumerate(map)) 沙发 本帖最后由 kinkon 于 2020-4-27 11:38 编辑
看了鱼友的发的发的帖子,发现差距还是很大的,都是快准狠,我只能暴力。
解法一:计数
def f384(arr):
carr = Counter(arr)
a0 = '0'* carr.get('z', 0)
a1 = '1'* min(carr.get('o', 0), carr.get('n', 0), carr.get('e', 0))
a2 = '2'* carr.get('w', 0)
a3 = '3'* min(carr.get('t', 0), carr.get('h', 0), carr.get('r', 0), carr.get('e', 0))
a4 = '4'* carr.get('u', 0)
a5 = '5'* min(carr.get('f', 0), carr.get('i', 0), carr.get('v', 0), carr.get('e', 0))
a6 = '6'* carr.get('x', 0)
a7 = '7'* min(carr.get('s', 0), carr.get('e', 0), carr.get('v', 0), carr.get('n', 0))
a8 = '8'* carr.get('g', 0)
a9 = '9'* min(carr.get('n', 0), carr.get('i', 0), carr.get('e', 0))
return a0 + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
解法二,时间O(N3),估计也超时了
def f384(arr):#带重复的暴力
dic = {'zero':'0', 'one':'1', 'two':'2', 'three':'3', 'four':'4',
'five':'5', 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9'}
carr = Counter(arr)
ans = ''
for val, n in dic.items():
if all(v in carr for v in val):
count = float('inf')
for v in val:
count = min(carr, count)
#print(v, carr, count)
ans += n * count
return ans
再来一个,还是暴力(有优化空间)
def f384(arr):
dic = {'zero':'0', 'one':'1', 'two':'2', 'three':'3', 'four':'4',
'five':'5', 'six':'6', 'seven':'7', 'eight':'8', 'nine':'9'}
carr = Counter(arr)
ans = ''
while carr:
for val, n in dic.items():
temp = Counter(val)
if not(temp - carr):
ans += n
carr -= temp
break
return ans Twilight6 发表于 2020-4-26 13:49
沙发
十一分钟过去了……还沙发呢 永恒的蓝色梦想 发表于 2020-4-26 13:56
十一分钟过去了……还沙发呢
{:10_277:}刚刚不小心看研究题目 才反应过来 本帖最后由 March2615 于 2020-4-26 14:31 编辑
def daily384(s: str) -> str:
# 解题思路:
# z只有zero,w只有two,u只有four,x只有six,g只有eight,...
# 寻找顺序:0(z)->2(w)->4(u)->6(x)->8(g)
# ->7(s-6(x))->5(f-4(u))->3(h-8(g))
# ->1(o-0(z)-2(w)-4(f))->9(i-5(f-4(u))-6(x)-8(g))
# 因为输入一定是正确的,所以只需要数特定字符的格数即可知道数字的个数
num = *10
num = s.count('z')
num = s.count('w')
num = s.count('u')
num = s.count('x')
num = s.count('g')
num = s.count('s') - num
num = s.count('f') - num
num = s.count("h") - num
num = s.count("o") - num - num - num
num = s.count("i") - num - num - num
return ''.join(str(i)*num for i in range(len(num)))
实在不好意思说动过脑了{:10_285:}
写的也太丑了,不过胜在好理解 想问一下,数字可以重复吗? Twilight6 发表于 2020-4-26 14:08
想问一下,数字可以重复吗?
可以 num_dict = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four',
5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}
def P384(s):
num_str = []
num_int = []
for i in s:
num_str.append(i)
for i in num_dict:
for each in num_dict:
if each not in num_str:
break
else:
for each in num_dict:
num_str.remove(each)
num_int.append(i)
for i in num_int:
print(i,end='')
s = input()
P384(s)
萌新求大佬赐教 本帖最后由 xiangjianshinan 于 2020-4-26 16:24 编辑
我也来:20200426 16:24
da = {0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
def timu384(s):
la = list(s)
outstr = ''
for i in range(10):
ba = True
while ba:
lta = la[:]
for j in da.get(i):
if j in lta:
lta.remove(j)
else:
ba= False
break
else:
outstr = outstr + str(i)
la = lta[:]
return outstr
print(timu384('owoztneoer'))
print(timu384('fviefuro'))
# 0->zero->z
# 2->two->w
# 4->four->u
# 6->six->x
# 8->eight->g
#
# 3->three-> t
# 5->five-> f
# 7->seven-> s
#
# 1->one-> -
# 9->nine->-
# 依据单词中出现的字母的独特性进行分类
# 一级特征字母为 z(0) w(2) u(4) x(6) g(8)
# 二级特征字母为 t(3) f(5) s(7)
# 三级特征字母为 o(1) i(9)
num_name = ['zero', 'one', 'two', 'three', 'four',
'five','six', 'seven', 'eight', 'nine']
def f384(s):
level_1 = {'z':0,'w':2,'u':4,'x':6,'g':8}
level_2 = {'t':3,'f':5,'s':7}
level_3 = {'o':1,'i':9}
res=""
(res,s)=remove_letter(level_1, s,res)
(res,s)=remove_letter(level_2, s,res)
(res,s)=remove_letter(level_3, s,res)
tmp = list(res)
tmp.sort()
res = "".join(tmp)
return res
def remove_letter(level,s,res):
for i in level:
if i in s:
res += str(level)
for j in num_name]:
s = s.replace(j, "", 1)
return (res,s)
if __name__ == "__main__":
s="fviefuro"
print(f384(s))
s="owoztneoer"
print (f384(s)) March2615 发表于 2020-4-26 14:04
实在不好意思说动过脑了
写的也太丑了,不过胜在好理解
这个思路巧哦,比暴力硬算要快的多,我也正想从字母下手呢 fan1993423 发表于 2020-4-26 18:16
这个思路巧哦,比暴力硬算要快的多,我也正想从字母下手呢
本来想写一个字符串相减的函数的,写着写着发现不如数个数来的快
幸好0就有特有的z,不然还真不一定想得到 def f384(s):
return '0'*s.count('z')+'1'*(s.count("o") - s.count('z')- s.count('w') - s.count('u'))+'2'*s.count('w')+'3'*(s.count("h") - s.count('g'))+'4'*s.count('u')+'5'*(s.count('f') - s.count('u'))+'6'*s.count('x')+'7'*(s.count('s') - s.count('x'))+'8'*s.count('g')+'9'*(s.count("i") - s.count('f') + s.count('u') -s.count('x') -s.count('g'))
占位 def f(s):
zero = s.count('z')
two = s.count('w')
four = s.count('u')
six = s.count('x')
eight = s.count('g')
one = s.count('o')
three = s.count('h')
five = s.count('f')
seven = s.count('s')
nine = s.count('i')
return '0'*zero+'1'*(one-four-two-zero)+'2'*two+'3'*(three-eight)+'4'*four+'5'*(five-four)+'6'*six+'7'*(seven-six)+'8'*eight+'9'*(nine-five-eight-six) kinkon 发表于 2020-4-26 13:55
看了鱼友的发的发的帖子,发现差距还是很大的,都是快准狠,我只能暴力。
解法一:计数
三段代码输入 "zeroonetwothreefourfivesixseveneightnine" 都有问题 March2615 发表于 2020-4-26 14:04
实在不好意思说动过脑了
写的也太丑了,不过胜在好理解
36 ms Frozen83 发表于 2020-4-26 15:01
萌新求大佬赐教
输入 'zerozero' 应该输出 '00'