zoukankan      html  css  js  c++  java
  • [LeetCode] Reconstruct Original Digits from English

    Reconstruct Original Digits from English

    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"

    Subscribe to see which companies asked this question.

    快两年没更新博客了,今天看了一下Leetcode,都500多道题了,就随便交了一道。

    开始考虑到用DFS,但是考虑到有些Digit可以由一个字母唯一确认,然后推算了一下,发现所有Digit都可以确定,就直接算答案了。好久没刷过题了,代码质量直线下降啊。

    class Solution {
    public:
        void update(int* count, string s, int n) {
            for (auto c : s) {
                count[c - 'a'] -= n;
            }
        }
    string originalDigits(string s) { vector<string> dict = {"zero", "one", "two", "three", "fore", "five", "six", "seven", "eight", "nine"}; int digitOrder[10] = {0, 8, 6, 2, 3, 7, 5, 4, 1, 9}; char keyOrder[10] = {'z', 'g', 'x', 'w', 'h', 's', 'v', 'f', 'o', 'i'}; int count[26] = {0}; for (auto c : s) { ++count[c - 'a']; } int res[10] = {0}; for (int i = 0; i < 10; ++i) { res[digitOrder[i]] = count[keyOrder[i] - 'a']; update(count, dict[digitOrder[i]], res[digitOrder[i]]); } string str = ""; for (int i = 0; i < 10; ++i) { for (int j = 0; j < res[i]; ++j) { str += ('0' + i); } } return str; } };
  • 相关阅读:
    linux创建用户和组
    ftp上来显示的时间和系统时间不一致
    在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
    关于业务主键和逻辑主键
    git push 提示
    浏览器默认样式
    css实现缩进无限嵌套
    使用设置报头x-Frame-Options限制iframe网页嵌套
    chrome控制台小技巧
    git版本库底层命令
  • 原文地址:https://www.cnblogs.com/easonliu/p/6689639.html
Copyright © 2011-2022 走看看