reconstruct-original-digits-from-english(好)

https://leetcode.com/problems/reconstruct-original-digits-from-english/

//https://discuss.leetcode.com/topic/63386/one-pass-o-n-java-solution-simple-and-clear

public class Solution {
    // zero one two three four five six seven eight nine ten
    // z 0
    // e 0 1 3 3 5 7 7 8 9
    // r 0 3 4
    // o 0 1 2 4
    // n 1 7 9 9
    // t 2 3 8
    // w 2
    // h 3 8
    // f 4 5
    // u 4
    // i 5 6 8 9
    // v 5 7
    // s 6 7
    // x 6
    // g 8

    public String originalDigits(String s) {
        // 太牛了,开始我也想到统计,但是想到删除字符串那些复杂的操作上面去了

        int[] counts = new int[10];
        for (int i=0; i<s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == ‘z‘) counts[0]++;
            if (ch == ‘u‘) counts[4]++;
            if (ch == ‘w‘) counts[2]++;
            if (ch == ‘x‘) counts[6]++;
            if (ch == ‘g‘) counts[8]++;
            if (ch == ‘h‘) counts[3]++; // 3, 8
            if (ch == ‘s‘) counts[7]++; // 6, 7
            if (ch == ‘f‘) counts[5]++; // 4, 5
            if (ch == ‘o‘) counts[1]++; // 0, 1, 2, 4
            if (ch == ‘i‘) counts[9]++; // 5, 6, 8, 9
        }

        counts[3] -= counts[8];
        counts[7] -= counts[6];
        counts[5] -= counts[4];
        counts[1] -= counts[0] + counts[2] + counts[4];
        counts[9] -= counts[5] + counts[6] + counts[8];

        StringBuilder sb = new StringBuilder();
        for (int i=0; i<10; i++) {
            for (int j=0; j<counts[i]; j++) {
                sb.append(i);
            }
        }
        return sb.toString();
    }
}
时间: 2024-12-20 03:29:46

reconstruct-original-digits-from-english(好)的相关文章

LeetCode 423. 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: Input contains only lowercase English letters. Input is guaranteed to be valid and can be transformed to its origina

423. Reconstruct Original Digits from English (leetcode)

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 origina

[LeetCode]423. 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: Input contains only lowercase English letters. Input is guaranteed to be valid and can be transformed to its origina

Leetcode: 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: Input contains only lowercase English letters. Input is guaranteed to be valid and can be transformed to its origina

[LeetCode] 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: Input contains only lowercase English letters. Input is guaranteed to be valid and can be transformed to its origina

[Swift]LeetCode423. 从英文中重建数字 | 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: Input contains only lowercase English letters. Input is guaranteed to be valid and can be transformed to its origina

Leetcode-423 Reconstruct Original Digits from English(从英文中重建数字)

1 class Solution 2 { 3 public: 4 string originalDigits(string s) 5 { 6 vector<int> CharacterList(26,0); 7 for(auto c:s) 8 { 9 CharacterList[c-'a'] ++; 10 } 11 12 string result; 13 if(CharacterList['g'-'a']) 14 { 15 result.append(CharacterList['g'-'a

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea