zoukankan      html  css  js  c++  java
  • 0423. Reconstruct Original Digits from English (M)

    Reconstruct Original Digits from English (M)

    题目

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.

    Note:

    1. Input contains only lowercase English letters.
    2. 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.
    3. Input length is less than 50,000.

    Example 1:

    Input: "owoztneoer"
    
    Output: "012"
    

    Example 2:

    Input: "fviefuro"
    
    Output: "45"
    

    题意

    将一个数字字符串中的每一个数字转化为英文,并打乱所有字符顺序,要求还原出数字字符串。

    思路

    找规律题。依照一定的顺序,每一个数字的英文中都有一个唯一的字符能代表这个英文,如'z'只能代表"zero"。可以发现有以下规律:

    1. 剩余数字:["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],其中具有特征字符的有:

      ('z', "zero"), ('w', "two"), ('u', "four"), ('x', "six"), ('g', "eight")

    2. 剩余数字:["one", "three", "five", "seven", "nine"],其中具有特征字符的有:

      ('o', "one"), ('h', "three"), ('f', "five")

    3. 剩余数字:["seven", "nine"],其中具有特征字符的有:

      ('s', "seven"), ('i', "nine")

    按照上述顺序转化即可得到答案。


    代码实现

    Java

    class Solution {
        public String originalDigits(String s) {
            StringBuilder sb = new StringBuilder();
            int[] digits = new int[10];
            int[] cnt = new int[26];
    
            for (char c : s.toCharArray()) {
                cnt[c - 'a']++;
            }
    
            handleDigit(digits, cnt, 0, "zero", 'z');
            handleDigit(digits, cnt, 2, "two", 'w');
            handleDigit(digits, cnt, 4, "four", 'u');
            handleDigit(digits, cnt, 6, "six", 'x');
            handleDigit(digits, cnt, 8, "eight", 'g');
            handleDigit(digits, cnt, 1, "one", 'o');
            handleDigit(digits, cnt, 3, "three", 'h');
            handleDigit(digits, cnt, 5, "five", 'f');
            handleDigit(digits, cnt, 7, "seven", 's');
            handleDigit(digits, cnt, 9, "nine", 'i');
    
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < digits[i]; j++) {
                    sb.append(i);
                }
            }
    
            return sb.toString();
        }
    
        private void handleDigit(int[] digits, int[] cnt,int digit, String word, char unique) {
            if (cnt[unique - 'a'] > 0) {
                int tmp = cnt[unique - 'a'];
                digits[digit] += tmp;
                for (char c : word.toCharArray()) {
                    cnt[c - 'a'] -= tmp;
                }
            }
        }
    }
    
  • 相关阅读:
    UVa 12716 GCD XOR (简单证明)
    2.12 运行计划并取得数据行
    nyoj 628 小媛在努力 【搜索】
    ArcGIS Server 10.2 公布Oracle11g数据源的 Feature Service
    项目复习期总结3:CSS引入方式,凝视,命名规范,背景,行高,文本属性
    Android使用有道翻译API实如今线翻译功能
    _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)
    夜&#183; 启程
    你不知道的JavaScript(六)Box&Unbox
    pugixml读取unicode编码的xml文件的做法
  • 原文地址:https://www.cnblogs.com/mapoos/p/14589137.html
Copyright © 2011-2022 走看看