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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yawn__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值