码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-10-16 21:38:11      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

 

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();
    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/charlesblc/p/5967694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!