0
点赞
收藏
分享

微信扫一扫

Leetcode_Python 423 从英文中重建数字


Leetcode_Python 423  从英文中重建数字_git

class Solution:
    def originalDigits(self, s: str) -> str:
        eposide = {"z":"zero","o":"one","w":"two","r":"three","u":"four",
           "f":"five","x":"six","s":"seven","g":"eight","i":"nine"}
        one_eposide = {"zero":"0","one":"1","two":"2","three":"3","four":"4",
               "five":"5","six":"6","seven":"7","eight":"8","nine":"9"}
        jihe = set(s)  #得到字符串所含有的字符,并统计字符的个数
        hash = {i:0 for i in jihe}  
        for i in s:
            hash["%s"%i] += 1
        str1 = ""
        for i in ["z","w","u","x","g","o","r","f","s","i"]:
            if i in jihe and hash[i] != 0:
                num = hash[i]
                str1 += one_eposide[eposide[i]] * num  #字母对应的数字
                for j in eposide[i]: #找出改元素对应的字母
                    hash[j] = hash[j] - num
        return "".join(sorted(str1))


举报

相关推荐

0 条评论