zoukankan      html  css  js  c++  java
  • leetcode 每日一题解题423. 从英文中重建数字

    423. 从英文中重建数字

    1,题目介绍

    给你一个字符串 s ,其中包含字母顺序打乱的用英文单词表示的若干数字(0-9)。按 升序 返回原始的数字。

    示例 1:
    
    输入:s = "owoztneoer"
    输出:"012"
    示例 2:
    
    输入:s = "fviefuro"
    输出:"45"
    
    提示:
    
    1 <= s.length <= 105
    s[i] 为 ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"] 这些字符之一
    s 保证是一个符合题目要求的字符串
    

    2,解题思路

    解题思路图

    我的解题代码:

    class Solution {
            public String originalDigits(String s) {
                if (s == null || "".equals(s)) {
                    return null;
                } else {
    
                    // 遍历所有字符个数
                    Map<Character, Integer> map = new HashMap<>(32);
                    for (int i = 0; i < s.length(); i++) {
                        char c = s.charAt(i);
                        if (map.containsKey(c)) {
                            map.put(c, map.get(c) + 1);
                        } else {
                            map.put(c, 1);
                        }
                    }
    
                    // 直接保存到
                    Integer[] integers = new Integer[10];
                    // zero
                    if (map.containsKey('z') && map.get('z') > 0) {
                        integers[0] = map.get('z');
                        map.put('e', map.get('e') - integers[0]);
                        map.put('r', map.get('r') - integers[0]);
                        map.put('o', map.get('o') - integers[0]);
                    }
    
                    // two
                    if (map.containsKey('w') && map.get('w') > 0) {
                        integers[2] = map.get('w');
                        map.put('t', map.get('t') - integers[2]);
                        map.put('o', map.get('o') - integers[2]);
                    }
    
                    // four
                    if (map.containsKey('u') && map.get('u') > 0) {
                        integers[4] = map.get('u');
                        map.put('f', map.get('f') - integers[4]);
                        map.put('o', map.get('o') - integers[4]);
                        map.put('r', map.get('r') - integers[4]);
                    }
    
                    // six
                    if (map.containsKey('x') && map.get('x') > 0) {
                        integers[6] = map.get('x');
                        map.put('s', map.get('s') - integers[6]);
                        map.put('i', map.get('i') - integers[6]);
                    }
    
                    // eight
                    if (map.containsKey('g') && map.get('g') > 0) {
                        integers[8] = map.get('g');
                        map.put('e', map.get('e') - integers[8]);
                        map.put('i', map.get('i') - integers[8]);
                        map.put('h', map.get('h') - integers[8]);
                        map.put('t', map.get('t') - integers[8]);
                    }
    
                    // one
                    if (map.containsKey('o') && map.get('o') > 0) {
                        integers[1] = map.get('o');
                        map.put('n', map.get('n') - integers[1]);
                        map.put('e', map.get('e') - integers[1]);
                    }
    
                    // three
                    if (map.containsKey('r') && map.get('r') > 0) {
                        integers[3] = map.get('r');
                        map.put('t', map.get('t') - integers[3]);
                        map.put('h', map.get('h') - integers[3]);
                        map.put('e', map.get('e') - integers[3] * 2);
                    }
    
                    // five
                    if (map.containsKey('f') && map.get('f') > 0) {
                        integers[5] = map.get('f');
                        map.put('i', map.get('i') - integers[5]);
                        map.put('v', map.get('v') - integers[5]);
                        map.put('e', map.get('e') - integers[5]);
                    }
    
                    // seven
                    if (map.containsKey('v') && map.get('v') > 0) {
                        integers[7] = map.get('v');
                        map.put('s', map.get('s') - integers[7]);
                        map.put('e', map.get('e') - integers[7] * 2);
                        map.put('n', map.get('n') - integers[7]);
                    }
    
                    // nine
                    if (map.containsKey('e') && map.get('e') > 0) {
                        integers[9] = map.get('e');
                    }
    
                    StringBuilder out = new StringBuilder();
                    for (int i = 0; i < 10; i++) {
                        if (integers[i] != null && integers[i] > 0) {
                            for (int j = 0; j < integers[i]; j++) {
                                out.append(i);
                            }
                        }
                    }
                    return out.toString();
                }
            }
        }
    

    3,官方解题思路与代码

    class Solution {
        public String originalDigits(String s) {
            Map<Character, Integer> c = new HashMap<Character, Integer>();
            for (int i = 0; i < s.length(); ++i) {
                char ch = s.charAt(i);
                c.put(ch, c.getOrDefault(ch, 0) + 1);
            }
    
            int[] cnt = new int[10];
            cnt[0] = c.getOrDefault('z', 0);
            cnt[2] = c.getOrDefault('w', 0);
            cnt[4] = c.getOrDefault('u', 0);
            cnt[6] = c.getOrDefault('x', 0);
            cnt[8] = c.getOrDefault('g', 0);
    
            cnt[3] = c.getOrDefault('h', 0) - cnt[8];
            cnt[5] = c.getOrDefault('f', 0) - cnt[4];
            cnt[7] = c.getOrDefault('s', 0) - cnt[6];
    
            cnt[1] = c.getOrDefault('o', 0) - cnt[0] - cnt[2] - cnt[4];
    
            cnt[9] = c.getOrDefault('i', 0) - cnt[5] - cnt[6] - cnt[8];
    
            StringBuffer ans = new StringBuffer();
            for (int i = 0; i < 10; ++i) {
                for (int j = 0; j < cnt[i]; ++j) {
                    ans.append((char) (i + '0'));
                }
            }
            return ans.toString();
        }
    }
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/solution/cong-ying-wen-zhong-zhong-jian-shu-zi-by-9g1r/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
    探究未知是最大乐趣
  • 相关阅读:
    SecureCRT
    NFS服务器搭建
    卸载oracle步骤
    redhat 5.5 x86_x64搭建samba服务器
    ORA-01940: cannot drop a user that is currently connected
    迁移表空间
    日常SQL使用总结
    DataGuard常规操作命令
    使用PowerDesginer画ER图
    数据库卸载
  • 原文地址:https://www.cnblogs.com/Mufasa/p/15600923.html
Copyright © 2011-2022 走看看