【Leetcode_总结】423. 从英文中重建数字 - python

本文介绍了一种算法,用于从打乱顺序的英文数字单词中还原出原始的数字序列。通过分析每个数字对应的唯一字符,利用计数和排除法,逐步确定每个数字的数量,最终输出升序排列的数字字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Q:

给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9。按升序输出原始的数字。

注意:

  1. 输入只包含小写英文字母。
  2. 输入保证合法并可以转换为原始的数字,这意味着像 "abc" 或 "zerone" 的输入是不允许的。
  3. 输入字符串的长度小于 50,000。

示例 1:

输入: "owoztneoer"

输出: "012" (zeroonetwo)

链接:https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/description/

思路:暴力求解,哈哈哈 超级长的代码 烂死了

代码:

class Solution(object):
    def originalDigits(self, s):
        """
        :type s: str
        :rtype: str
        """
        from collections import Counter
        dic = Counter(s)
        zero = ""
        one = ""
        two= ""
        three = ""
        four  = ""
        five  = ""
        six= ""
        seven= ""
        eight= ""
        nine= ""
        if "z" in dic:
            zero = "0" * dic["z"]
            tmp = dic["z"]
            for w in "zero":
                dic[w] -= tmp
        if "w" in dic:
            two = "2" * dic["w"]
            tmp = dic["w"]
            for w in "two":
                dic[w] -= tmp
        if "x" in dic:
            six = "6" * dic["x"]
            tmp = dic["x"]
            for w in "six":
                dic[w] -= tmp
        if "g" in dic:
            eight = "8" * dic["g"]
            tmp = dic["g"]
            for w in "eight":
                dic[w] -= tmp
        if "t" in dic and dic["t"] != 0:
            three = "3" * dic["t"]
            tmp = dic["t"]
            for w in "three":
                dic[w] -= tmp

        if "r" in dic and dic["r"] != 0:
            four = "4" * dic["r"]
            tmp = dic["r"]
            for w in "four":
                dic[w] -= tmp
        if "f" in dic and dic["f"] != 0:
            five = "5" * dic["f"]
            tmp = dic["f"]
            for w in "five":
                dic[w] -= tmp

        if "s" in dic and dic["s"] != 0:
            seven = "7" * dic["s"]
            tmp = dic["s"]
            for w in "seven":
                dic[w] -= tmp

        if "i" in dic and dic["i"] != 0:
            nine = "9" * dic["i"]
            tmp = dic["i"]
            for w in "nine":
                dic[w] -= tmp

        if "o" in dic and dic["o"] != 0:
            one = "1" * dic["o"]
            tmp = dic["o"]
            for w in "one":
                dic[w] -= tmp
        return zero + one + two + three + four + five + six + \
               seven + eight + nine

附一个别人的吧,思路是差不多的

class Solution(object):
    def originalDigits(self, s):
        """
        :type s: str
        :rtype: str
        """
        ret = ""
        nums = [0]*10
        nums[0] = s.count("z")
        nums[2] = s.count("w")
        nums[4] = s.count("u")
        nums[6] = s.count("x")
        nums[8] = s.count("g")
        nums[7] = s.count("s") - nums[6]
        nums[5] = s.count("f") - nums[4]
        nums[3] = s.count("h") - nums[8]
        nums[1] = s.count("o") - nums[0] - nums[2] - nums[4]
        nums[9] = s.count("i") - nums[5] - nums[6] - nums[8]
        for i in range(0,10):
            ret += str(i)*nums[i]
        return ret
            

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值