Leetcode: 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"

Career Cup 150 Pg 442

Think of Convert(19,323,984) = Process(19) + "million" + Process(323) + "thousand" + Process(984) + ""

The Process is a process that generates words representation for integer below 1000

 1 public class Solution {
 2     String[] digits = new String[]{"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
 3     String[] teen = new String[]{"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
 4     String[] tens = new String[]{"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
 5     String[] bigs = new String[]{"", "Thousand", "Million", "Billion"};
 6
 7     public String numberToWords(int num) {
 8         String res = new String();
 9         if (num == 0) return "Zero";
10         int count = 0;
11         while (num > 0) {
12             int belowThousand = num % 1000;
13             if (belowThousand != 0) {
14                 res = process(belowThousand) + " " + bigs[count] + " " + res;
15             }
16             count++;
17             num = num / 1000;
18         }
19         return res.trim();
20     }
21
22     public String process(int n) {
23         String res = new String();
24         if (n/100 > 0) {
25             res = digits[n/100-1] + " " + "Hundred" + " ";
26             n = n%100;
27         }
28         if (n>=11 && n<=19) {
29             res = res + teen[n%10-1];
30             return res;
31         }
32         else if (n/10 > 0) {
33             res = res + tens[n/10-1] + " ";
34             n = n%10;
35         }
36         if (n > 0) {
37             res = res + digits[n-1];
38         }
39         return res.trim();
40     }
41 }
时间: 2024-08-07 00:08:44

Leetcode: Integer to English Words的相关文章

[LeetCode] 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 -&g

LeetCode:Integer to English Words - 按英语读法输出数字对应单词

1.题目名称 Integer to English Words(按英语读法输出数字对应单词) 2.题目地址 https://leetcode.com/problems/integer-to-english-words/ 3.题目内容 英文:Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 中文:给出一个非负整数

leetCode 273. Integer to English Words 字符串 | Hard

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 Hundre

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

leetcode-【hard】273. Integer to English Words

题目: 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 Hu

273. Integer to English Words

// 看看人家的转换int为String吧,真的很不错啊!主要是在1000以内的数自由转换的时候我没做好 private static final String[] lessThan20 = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",

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 -&g

[LeetCode 273] Integer to English Word

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 -&g

Integer to English word leetcode java

问题描述: 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" 12345