LeetCode273. 整数转换英文表示(Java)

mac2024-07-05  54

题目:

  将非负整数转换为其对应的英文表示。可以保证给定输入小于 231 - 1 。

示例 1: 输入: 123 输出: "One Hundred Twenty Three" 示例 2: 输入: 12345 输出: "Twelve Thousand Three Hundred Forty Five" 示例 3: 输入: 1234567 输出: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 示例 4: 输入: 1234567891 输出: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

题解:

非负数包含了0。231 - 1 表明在int类型的数据范围之内。数字翻译成英文时候,一般3个数为一组,单位分别为千、百万、十亿。转换时候注意每次将一个三位数转换成为英文,然后加上3中的单位。

代码:

public class EnglishTrans{ public static void main(String[] args) { // 2134569087 // 2100000000 int num = 2134569087; String str = numberToWords(num); System.out.println("***********************************************************************************************"); System.out.println("翻译:" + str); System.out.println("***********************************************************************************************"); } public static String numberToWords(int num) { if (num == 0) { return "Zero"; } // 循环次数标记 int flag = 0; StringBuilder builder = new StringBuilder(); while (num > 0) { int temp = num % 1000; // 为0时候不做计算 if (temp != 0) { if (flag == 0) { // 将三位数转换成为英文,在加上单位 builder.append(transLowThousandToEnglish(temp)); } else { StringBuilder sb = new StringBuilder(); StringBuilder append = sb.append(transLowThousandToEnglish(temp)).append(" ").append(map().get((int) Math.pow(1000, flag))).append(" "); // 因为是从个位算起,故将后得到的翻译置于首位 builder.insert(0, append); } } flag++; // 翻译目前最低的三位后,舍弃后三位 num /= 1000; } return builder.toString().trim(); } /** * 构建中英文对应的map */ private static Map<Integer, String> map() { Map<Integer, String> map = new HashMap<>(32); map.put(0, "Zero"); map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three"); map.put(4, "Four"); map.put(5, "Five"); map.put(6, "Six"); map.put(7, "Seven"); map.put(8, "Eight"); map.put(9, "Nine"); map.put(10, "Ten"); map.put(11, "Eleven"); map.put(12, "Twelve"); map.put(13, "Thirteen"); map.put(14, "Fourteen"); map.put(15, "Fifteen"); map.put(16, "Sixteen"); map.put(17, "Seventeen"); map.put(18, "Eighteen"); map.put(19, "Nineteen"); map.put(20, "Twenty"); map.put(30, "Thirty"); map.put(40, "Forty"); map.put(50, "Fifty"); map.put(60, "Sixty"); map.put(70, "Seventy"); map.put(80, "Eighty"); map.put(90, "Ninety"); map.put(100, "Hundred"); // 千,百万,十亿 map.put(1000, "Thousand"); map.put(1000000, "Million"); map.put(1000000000, "Billion"); return map; } /** * 将一个三位数转化为英文,因为这个三位数可能在比较大的数的中间:如(1,000,369,124)中的369,所以为0时不能返回zero */ private static String transLowThousandToEnglish(int num) { // 不能返回Zero if (num == 0) { return ""; } // 顺序从小到大比较好 if (num <= 20) { return map().get(num); } else if (num > 20 && num < 100) { StringBuilder sb = new StringBuilder(); // 十位数 sb.append(map().get(num / 10 * 10)); // 个位数 int units = num % 10; if (units != 0) { sb.append(" ").append(map().get(units)); } return sb.toString(); } else if (num >= 100 && num < 1000) { StringBuilder sb = new StringBuilder(); // 百位数 sb.append(map().get(num / 100)).append(" ").append("Hundred"); int tensDigit = num % 100; // 十位数 if (tensDigit > 0 && tensDigit <= 20) { sb.append(" ").append(map().get(tensDigit)); } else if (tensDigit > 20 && tensDigit < 100) { sb.append(" ").append(map().get(tensDigit / 10 * 10)); int units = tensDigit % 10; // 个位数 if (units > 0) { sb.append(" ").append(map().get(units)); } } return sb.toString(); } else { return "ERROR"; } } }

运行

最新回复(0)