Leetcode 273: Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

 

 

  1 public class Solution {
  2     public string NumberToWords(int num) {
  3         if (num == 0) return "Zero";
  4 
  5         InitMap();
  6         
  7         var sb = new StringBuilder();
  8         if (num >= 1000000000)
  9         {
 10             sb.Append(ConvertHundred(num / 1000000000));
 11             num %= 1000000000;
 12             sb.Append(" Billion");
 13         }    
 14         
 15         if (num >= 1000000)
 16         {
 17             if (sb.Length > 0) sb.Append(" ");
 18             sb.Append(ConvertHundred(num / 1000000));
 19             num %= 1000000;
 20             sb.Append(" Million");
 21         }
 22         
 23         if (num >= 1000)
 24         {
 25             if (sb.Length > 0) sb.Append(" ");
 26             sb.Append(ConvertHundred(num / 1000));
 27             num %= 1000;
 28             sb.Append(" Thousand");
 29         }
 30         
 31         if (num > 0)
 32         {
 33             if (sb.Length > 0) sb.Append(" ");
 34             sb.Append(ConvertHundred(num));
 35         }
 36         
 37         return sb.ToString();        
 38     }
 39     
 40     private Dictionary<int, string> map = new Dictionary<int, string>();
 41     
 42     private void InitMap()
 43     {
 44         map[1] = "One";
 45         map[2] = "Two";
 46         map[3] = "Three";
 47         map[4] = "Four";
 48         map[5] = "Five";
 49         map[6] = "Six";
 50         map[7] = "Seven";
 51         map[8] = "Eight";
 52         map[9] = "Nine";
 53         map[10] = "Ten";
 54         map[11] = "Eleven";
 55         map[12] = "Twelve";
 56         map[13] = "Thirteen";
 57         map[14] = "Fourteen";
 58         map[15] = "Fifteen";
 59         map[16] = "Sixteen";
 60         map[17] = "Seventeen";
 61         map[18] = "Eighteen";
 62         map[19] = "Nineteen";
 63         map[20] = "Twenty";
 64         map[30] = "Thirty";
 65         map[40] = "Forty";
 66         map[50] = "Fifty";
 67         map[60] = "Sixty";
 68         map[70] = "Seventy";
 69         map[80] = "Eighty";
 70         map[90] = "Ninety";
 71     }
 72     
 73     private string ConvertHundred(int num)
 74     {
 75         var sb = new StringBuilder();
 76         
 77         if (num >= 100)
 78         {
 79             sb.Append(map[num/100]);
 80             sb.Append(" Hundred");
 81             num %= 100;
 82         }
 83         
 84         if (num >= 20)
 85         {
 86             if (sb.Length > 0) sb.Append(" ");
 87             sb.Append(map[(num/10) * 10]);
 88             num %= 10;
 89         }
 90         
 91         if (num >= 10)
 92         {
 93             if (sb.Length > 0) sb.Append(" ");
 94             sb.Append(map[num]);
 95             num = 0;
 96         }
 97         
 98         if (num > 0)
 99         {
100             if (sb.Length > 0) sb.Append(" ");
101             sb.Append(map[num]);
102         }
103         
104         return sb.ToString();
105     }
106 }

 

 

 

转载于:https://www.cnblogs.com/liangmou/p/7995996.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值