题目链接: 整数转换英文表示
有关题目
将非负整数 num 转换为其对应的英文表示。
示例 1:
输入:num = 123
输出:"One Hundred Twenty Three"
示例 2:
输入:num = 12345
输出:"Twelve Thousand Three Hundred Forty Five"
示例 3:
输入:num = 1234567
输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
示例 4:
输入:num = 1234567891
输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
提示:
0 <= num <= 2^31 - 1
题解
法一:递归
参考官方题解
class Solution {
public:
vector<string> singles = {
"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
vector<string> teens = {
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> tens = {
"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy"

这篇博客介绍了如何将非负整数转换为英文表示的方法,包括两种不同的实现方式:递归和迭代。提供了详细的代码实现,覆盖从个位到亿位的转换,并给出了多个示例验证算法的正确性。对于输入的0到2^31-1之间的整数,都能准确转换成英文单词形式。
最低0.47元/天 解锁文章
1073

被折叠的 条评论
为什么被折叠?



