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   private final String[] belowTwenty = new String[] {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", 
 3           "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
 4   private final String[] belowHundred = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
 5   
 6   public String numberToWords(int num) {
 7       if (num == 0) {
 8           return "Zero";
 9       }
10       return helper(num);
11   }
12 
13   private String helper(int num) {
14       String result = new String();
15       if (num < 20) { result = belowTwenty[num]; }
16       else if (num < 100) { result = belowHundred[num / 10] + " " + helper(num % 10); }
17       else if (num < 1000) { result = helper(num / 100) + " Hundred " +  helper(num % 100); }
18       else if (num < 1000000) { result = helper(num / 1000) + " Thousand " +  helper(num % 1000); }
19       else if (num < 1000000000) { result = helper(num / 1000000) + " Million " +  helper(num % 1000000); }
20       else result = helper(num / 1000000000) + " Billion " + helper(num % 1000000000);
21       return result.trim();
22   }
23 }

Reference:

http://blog.welkinlan.com/2015/09/29/integer-to-english-words-leetcode-java/

English words to Integer

 1 static long englishWordsToNumber(String input) {
 2         long result = 0;
 3         long finalResult = 0;
 4         
 5 
 6         if (input != null && input.length() > 0) {
 7             input = input.replaceAll("-", " ");
 8             input = input.toLowerCase().replaceAll(" and", " ");
 9             String[] splittedParts = input.trim().split("\\s+");
10             for (String str : splittedParts) {
11                 if (str.equalsIgnoreCase("zero")) {
12                     result += 0;
13                 } else if (str.equalsIgnoreCase("one")) {
14                     result += 1;
15                 } else if (str.equalsIgnoreCase("two")) {
16                     result += 2;
17                 } else if (str.equalsIgnoreCase("three")) {
18                     result += 3;
19                 } else if (str.equalsIgnoreCase("four")) {
20                     result += 4;
21                 } else if (str.equalsIgnoreCase("five")) {
22                     result += 5;
23                 } else if (str.equalsIgnoreCase("six")) {
24                     result += 6;
25                 } else if (str.equalsIgnoreCase("seven")) {
26                     result += 7;
27                 } else if (str.equalsIgnoreCase("eight")) {
28                     result += 8;
29                 } else if (str.equalsIgnoreCase("nine")) {
30                     result += 9;
31                 } else if (str.equalsIgnoreCase("ten")) {
32                     result += 10;
33                 } else if (str.equalsIgnoreCase("eleven")) {
34                     result += 11;
35                 } else if (str.equalsIgnoreCase("twelve")) {
36                     result += 12;
37                 } else if (str.equalsIgnoreCase("thirteen")) {
38                     result += 13;
39                 } else if (str.equalsIgnoreCase("fourteen")) {
40                     result += 14;
41                 } else if (str.equalsIgnoreCase("fifteen")) {
42                     result += 15;
43                 } else if (str.equalsIgnoreCase("sixteen")) {
44                     result += 16;
45                 } else if (str.equalsIgnoreCase("seventeen")) {
46                     result += 17;
47                 } else if (str.equalsIgnoreCase("eighteen")) {
48                     result += 18;
49                 } else if (str.equalsIgnoreCase("nineteen")) {
50                     result += 19;
51                 } else if (str.equalsIgnoreCase("twenty")) {
52                     result += 20;
53                 } else if (str.equalsIgnoreCase("thirty")) {
54                     result += 30;
55                 } else if (str.equalsIgnoreCase("forty")) {
56                     result += 40;
57                 } else if (str.equalsIgnoreCase("fifty")) {
58                     result += 50;
59                 } else if (str.equalsIgnoreCase("sixty")) {
60                     result += 60;
61                 } else if (str.equalsIgnoreCase("seventy")) {
62                     result += 70;
63                 } else if (str.equalsIgnoreCase("eighty")) {
64                     result += 80;
65                 } else if (str.equalsIgnoreCase("ninety")) {
66                     result += 90;
67                 } else if (str.equalsIgnoreCase("hundred")) {
68                     result *= 100;
69                 } else if (str.equalsIgnoreCase("thousand")) {
70                     result *= 1000;
71                     finalResult += result;
72                     result = 0;
73                 } else if (str.equalsIgnoreCase("million")) {
74                     result *= 1000000;
75                     finalResult += result;
76                     result = 0;
77                 } else if (str.equalsIgnoreCase("billion")) {
78                     result *= 1000000000;
79                     finalResult += result;
80                     result = 0;
81                 } else if (str.equalsIgnoreCase("trillion")) {
82                     result *= 1000000000000L;
83                     finalResult += result;
84                     result = 0;
85                 }
86             }
87 
88             finalResult += result;
89             result = 0;
90         }
91         return finalResult;
92     }

 

posted @ 2016-07-28 05:27  北叶青藤  阅读(241)  评论(0)    收藏  举报