Leetcode——整数转换英文表示

Posted Yawn,

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode——整数转换英文表示相关的知识,希望对你有一定的参考价值。

1. 整数转换英文表示

(1)字符串模拟

英文中数字表示是每三位一组进行的,

  • x>=100:此时首先需要表示成 ??? hundred,表示完后考虑更小的位数;
  • x >= 20:此时需要表示成 ??? XXX-ty 的形式,表示完后考虑更小的位数;
  • x < 20:直接描述成具体的单词。
class Solution {
    private static int BILLION = (int)Math.pow(10, 9);
	private static int MILLION = (int)Math.pow(10, 6);
	private static int THOUSAND = (int)Math.pow(10, 3);
	private static String[] singles = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"
			, "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen"
			, "Seventeen", "Eighteen", "Nineteen"};
	private static String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
	
	public static String numberToWords(int num) {
		if(num == 0) {
			return "Zero";
		}
		StringBuilder sb = new StringBuilder();
		while(num > 0) {
			if(num >= BILLION) {
				sb.append(getStrInHundreds(num / BILLION)).append(" Billion");
				num %= BILLION;
			} else if(num >= MILLION) {
				sb.append(getStrInHundreds(num / MILLION)).append(" Million");
				num %= MILLION;
			} else if(num >= THOUSAND) {
				sb.append(getStrInHundreds(num / THOUSAND)).append(" Thousand");
				num %= THOUSAND;
			} else {
				sb.append(getStrInHundreds(num));
				num = 0;
			}
			if(num > 0) {
				sb.append(' ');
			}
		}
		return sb.toString();
	}
	
	private static String getStrInHundreds(int num) {
		StringBuilder sb = new StringBuilder();
		while(num > 0) {
			if(num >= 100) {
				sb.append(singles[num / 100]).append(" Hundred");
				num %= 100;
			} else if(num >= 20) {
				sb.append(tens[num / 10]);
				num %= 10;
			} else {
				sb.append(singles[num]);
				num = 0;
			}
			if(num > 0) {
				sb.append(' ');
			}
		}
		return sb.toString();
	}
}

(2)递归

class Solution {
   
    private static final String[] digitsSet1 = {"Zero ","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine ","Ten ","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
    private static final String[] digitsSet2 = {"Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
    
    private String numberToWords1(int num){
        if(num >= 1000000000) 
            return numberToWords1(num / 1000000000) + "Billion " + numberToWords1(num % 1000000000);
        if(num >= 1000000) 
            return numberToWords1(num / 1000000) + "Million " + numberToWords1(num % 1000000);
        if(num >= 1000) 
            return numberToWords1(num / 1000) + "Thousand " + numberToWords1(num % 1000);
        if(num >= 100) 
            return numberToWords1(num / 100) + "Hundred " + numberToWords1(num % 100);
        if(num == 0) 
            return "";
        if(num <= 19) 
            return digitsSet1[num];
        if(num % 10 == 0) 
            return digitsSet2[num / 10 - 2];
        return digitsSet2[num / 10 - 2] + digitsSet1[num % 10];
    }
    
    public String numberToWords(int num) {
        if(num == 0) return "Zero";
        return numberToWords1(num).trim();
    }
}

以上是关于Leetcode——整数转换英文表示的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode——整数转换英文表示

Leetcode——整数转换英文表示

LeetCode 273. 整数转换英文表示 / 29. 两数相除 / 412. Fizz Buzz

《LeetCode之每日一题》:175.数字转换英文表示

leetcode每日打卡--476. 数字的补数

[LeetCode] 273. Integer to English Words