[LeetCode] 273. 整数转换英文表示

警告
本文最后更新于 2021-10-11,文中内容可能已过时。

非负整数 num 转换为其对应的英文表示。

示例 1:

1
2
输入:num = 123
输出:"One Hundred Twenty Three"

示例 2:

1
2
输入:num = 12345
输出:"Twelve Thousand Three Hundred Forty Five"

示例 3:

1
2
输入:num = 1234567
输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

示例 4:

1
2
3

输入:num = 1234567891
输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

提示

0 <= num <= 231 - 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/integer-to-english-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

英文表示为三个一组,如 123_456_789 为 123 百万 456 千 789 。根据范围提示,最大约为 21 亿, 即 2 十亿(Billion)。因此有以下代码,实际上还可以再抽象一下,但是我没再改。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
impl Solution {
    pub fn number_to_words(num: i32) -> String {
                if num == 0 { return "Zero".to_string() }
        let mut finl = String::new();
        match num {
            1..=999 => finl=deal(num),
            1_000..=999_999 => {
                finl = deal(num/1_000)+" Thousand";

                let num = num % 1_000; // 化1000内
                if num!=0 { 
                    finl+=" ";
                    finl+=&deal(num);
                }
            },
            1_000_000..=999_999_999 => { 
                finl = deal(num/1_000_000)+" Million";

                let num = num % 1_000_000; // 化1_000_000内
                if num/1_000!=0 { 
                    finl+=" ";
                    finl += &(deal(num/1_000)+" Thousand");
                }
                

                let num = num % 1_000; // 化1000内
                if num!=0 { 
                    finl+=" ";
                    finl+=&deal(num);
                }
            }
            1_000_000_000..=2_147_483_647 => {
                finl = deal(num/1_000_000_000)+" Billion";

                let num = num % 1_000_000_000; // 化1_000_000_000内
                if num/1_000_000!=0 { 
                    finl+=" ";
                    finl += &(deal(num/1_000_000)+" Million");
                }
                let num = num % 1_000_000; // 化1_000_000内
                if num/1_000!=0 { 
                    finl+=" ";
                    finl += &(deal(num/1_000)+" Thousand");
                }
                let num = num % 1_000; // 化1000内
                if num!=0 { 
                    finl+=" ";
                    finl+=&deal(num);
                }
            }
            _ => {}
        }
        return finl;
        fn deal(num:i32) -> String {
            use std::collections::HashMap;
            let vec1 = vec!(1,2,3,4,5,6,7,8,9);
            let vec2 = vec!("One","Two","Three","Four","Five","Six","Seven","Eight","Nine");
            let map1:HashMap<_,_> =vec1.iter().zip(vec2.iter()).collect();
            let vec3 = vec!(10,11,12,13,14,15,16,17,18,19);
            let vec4 = vec!("Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen", "Seventeen","Eighteen", "Nineteen");
            let map2:HashMap<_,_> =vec3.iter().zip(vec4.iter()).collect();
            let vec5= vec!(20,30,40,50,60,70,80,90);
            let vec6 = vec!("Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety");
            let map3:HashMap<_,_> =vec5.iter().zip(vec6.iter()).collect();
            let mut ans3 = String::new();
            if num >=100 { 
                let p3 = num/100;
                ans3=(**map1.get(&p3).unwrap()).to_string()+" Hundred";
            }
            let mut ans2 = String::new();
            let num = num%100;
            let p2 = num;
            match p2 {
                0 => {}
                1..=9 => { ans3+=" ";ans2 = (**map1.get(&p2).unwrap()).to_string() }
                10..=19 => { ans3+=" ";ans2 =(**map2.get(&p2).unwrap()).to_string() }
                _ => { 
                    let tem = p2/10*10;
                    ans3+=" ";
                    ans2 = (**map3.get(&tem).unwrap()).to_string();
                    let tem = p2%10;
                    match tem {
                        0 => {},
                        _ => {ans2+=" ";ans2 += *map1.get(&tem).unwrap()},
                    }
                }
            }
            if ans3.len()==1 { ans3="".to_string()}
            format!("{}{}", ans3, ans2)
        }
    }
}