[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”

解题思路

略。

实现代码

// Runtime: 8 ms
class Solution {
public:
    string numberToWords(int num) {
        return num2str(num);
    }

private:
    string num2str(int num)
    {
        string res = "";
        if (num <= 19)
        {
            res += nums[num];
        }
        else if (num <= 99)
        {
            res += tens[num / 10];
            if (num % 10 != 0)
            {
                res += " ";
                res += num2str(num % 10);
            }
        }
        else if (num <= 999)
        {
            res += num2str(num / 100);
            res += " Hundred";
            if (num % 100 != 0)
            {
                res += " ";
                res += num2str(num % 100);
            }
        }
        else if (num <= 999999)
        {
            res += num2str(num / 1000);
            res += " Thousand";
            if (num % 1000 != 0)
            {
                res += " ";
                res += num2str(num % 1000);
            }
        }
        else if (num <= 999999999)
        {
            res += num2str(num / 1000000);
            res += " Million";
            if (num % 1000000 != 0)
            {
                res += " ";
                res += num2str(num % 1000000);
            }
        }
        else if (num <= 999999999999)
        {
            res += num2str(num / 1000000000);
            res += " Billion";
            if (num % 1000000000 != 0)
            {
                res += " ";
                res += num2str(num % 1000000000);
            }
        }

        return res;
    }

    string tens[10] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    string nums[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
        "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen",
        "Eighteen", "Nineteen"};
};
时间: 2024-10-21 23:57:43

[LeetCode] Integer to English Words的相关文章

[LeetCode] Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

java io 根据TXT 在控制台上输出相关表的信息

import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.text.DecimalFormat; impor

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode]8. String to Integer (atoi)

[题目] 点击打开链接 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this probl

[LeetCode]13.Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [分析] 这是一种首先会想到的思路:一个一个字母解析,先解析是不是双字母罗马数字,如果不是就按单字母罗马数字解析. [代码] /********************************* * 日期:2015-01-22 * 作者:SJF0115 * 题目: 13.R

LeetCode 7 Reverse Integer(翻转整数)

翻译 翻转一个整型数 例1:x = 123, 返回 321 例2:x = -123, 返回 -321 原文 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? (来自LeetCode官网) Here are some good questions to ask before coding. Bonus poi

LeetCode 12 Integer to Roman(整型数到罗马数)

翻译 给定一个整型数值,将其转换到罗马数字. 输入被保证在1到3999之间. 原文 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 我不会告诉你一开始我是用的无数个变量和if-- 后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了. 为了让大家理解罗马数是怎么计数的,这里我截了一张图,具体的大家可以自行用微软Bing

[LeetCode]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. [分析] I = 1; V = 5; X = 10; L = 50; C = 100; D = 500; M = 1000; 还有一些特殊的:每两个阶段的之间有一个减法的表示,比如900=CM, C写在M前面表示M-C. 求商得到每个罗马文字的个数(如:3999  / 10