Leetcode 423. Reconstruct Original Digits from English 重构数字 解题报告

1 解题思想

这道题是让我们给了我们一个字符串,字符串里全都是用英文写的数字,但是字母顺序是打乱的

现在我们需要还原出来其真正的数字,字符串肯定是合法的

解题的核心在于,0~9对应的英文字母,有那么些规律,可以从中找到相关的规律。

* zero:0
     * one:1
     * two:2
     * three:3
     * four:4
     * five:5
     * six:6
     * seven:7
     * eight:8
     * nine:9
     * 
     * 我们可以观察得到:
     * 只有Zero:包含z,剩下的都没有
     * 只有Two:包含w,其他都没有
     * 除了0,2,只有six包含x
     * 除了0,2,6,只有.... 等等以此类推
     * 
     * 找到每一个规律后,就可以开始了,这里我就不详细写了

2 原题

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"

3 AC解

public class Solution {
    public String originalDigits(String s) {
        /**
         * 字符串表示了一些英文的数字,但是已经乱序了,现在要求将其转化为具体的数值
         * 
         * 
         * 再次特别注明:我发代码的时候是题目当天出来的时候,已经AC了,但是Leetcode可能会更改测试用例,导致代码无法AC
         * 如果出现上述情况,请给我留言,我会修正更新代码
         * 
         * zero:0
         * one:1
         * two:2
         * three:3
         * four:4
         * five:5
         * six:6
         * seven:7
         * eight:8
         * nine:9
         * 
         * 我们可以观察得到:
         * 只有Zero:包含z,剩下的都没有
         * 只有Two:包含w,其他都没有
         * 除了0,2,只有six包含x
         * 除了0,2,6,只有.... 等等以此类推
         * 
         * 找到每一个规律后,就可以开始了,这里我就不详细写了
         * 
         * */
         //只包含0~9,我直接if了
         char chars[] = s.toCharArray();
         int n = s.length();
         int[] count = new int[10];
         for (int i = 0; i < n; i++){
            char c = s.charAt(i);
            if (c == 'z') count[0]++; //只有0有z
            else if (c == 'w') count[2]++; // 2 -> w
            else if (c == 'x') count[6]++; // 6 -> x
            else if (c == 'g') count[8]++; // 8-> g
            else if (c == 'u') count[4]++; // 4 -> u
            //上面是独有的,下面是可以计算出来的
            else if (c == 's') count[7]++; // 6/7 ->s
            else if (c == 'f') count[5]++; // 4/5 -> f
            else if (c == 'h') count[3]++; // 3/8 - > h
            else if (c == 'i') count[9]++; // 5/6/8/9 ->i
            else if (c == 'o') count[1]++; // 0/1/2/4 ->o
         }
         //计算
         count[7] -= count[6];
         count[5] -= count[4];
         count[3] -= count[8];
         count[9] = count[9] - count[8] - count[5] - count[6];
         count[1] = count[1] - count[0] - count[2] - count[4];
         StringBuilder sb = new StringBuilder();
         for (int i = 0; i <= 9; i++){
            for (int j = 0; j < count[i]; j++){
                sb.append(i);
            }
         }
        return sb.toString();


    }
}

再次特别注明:我发代码的时候是题目当天出来的时候,已经AC了,但是Leetcode可能会更改测试用例,导致代码无法AC

这里写图片描述
如果出现上述情况,请给我留言,我会修正更新代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值