博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】273. Integer to English Words
阅读量:7043 次
发布时间:2019-06-28

本文共 2883 字,大约阅读时间需要 9 分钟。

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"

 

 

 先把数字分割成4个三分位处理。前3个三分位分别加上Billion,Million,Thousand。
三分位处理函数为Helper()
class Solution {public:    string Helper(string numStr, string dict1[], string dict2[], string dict3[])    {        int num = atoi(numStr.c_str());        if(num == 0)            return "";        else        {            string ret = "";            int hundred = numStr[0] - '0';            int ten = numStr[1] - '0';            int one = numStr[2] - '0';                        if(hundred != 0)                ret += dict1[hundred-1] + " Hundred";            if(ten == 0)            {                if(one == 0)                    return ret;                else                    return (ret=="")?(dict1[one-1]):(ret+" "+dict1[one-1]);             }            else if(ten == 1)            {                return (ret=="")?(dict2[one]):(ret+" "+dict2[one]);             }            else            {                if(one == 0)                    return (ret=="")?(dict3[ten-2]):(ret+" "+dict3[ten-2]);                else                    return (ret=="")?(dict3[ten-2]+" "+dict1[one-1]):(ret+" "+dict3[ten-2]+" "+dict1[one-1]);            }        }    }    string numberToWords(int num) {        if(num == 0)            return "Zero";        else        {            string ret = "";            string dict1[9] = {
"One","Two","Three","Four","Five","Six","Seven","Eight","Nine"}; string dict2[10] = {
"Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen", "Eighteen","Nineteen"}; string dict3[8] = {
"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}; string numStr = to_string(num); int len = numStr.size(); string padding = string(12-len, '0'); numStr = padding + numStr; string billionStr = Helper(numStr.substr(0,3), dict1, dict2, dict3); if(billionStr != "") ret += billionStr + " Billion"; string millionStr = Helper(numStr.substr(3,3), dict1, dict2, dict3); if(millionStr != "") ret += (ret=="")?(millionStr+" Million"):(" "+millionStr+" Million"); string thousandStr = Helper(numStr.substr(6,3), dict1, dict2, dict3); if(thousandStr != "") ret += (ret=="")?(thousandStr+" Thousand"):(" "+thousandStr+" Thousand"); string oneStr = Helper(numStr.substr(9,3), dict1, dict2, dict3); if(oneStr != "") ret += (ret=="")?(oneStr):(" "+oneStr); return ret; } }};

 

转载地址:http://jvxal.baihongyu.com/

你可能感兴趣的文章
Python基础之继承与派生
查看>>
filter、map、every函数的使用
查看>>
黑马程序员——iOS学习——UITableView表视图单元样式
查看>>
Bash基础——减号-
查看>>
Android适配文件dimen自动生成代码
查看>>
走马观花--快餐学python笔记
查看>>
jquery轻量级富文本编辑器Trumbowyg
查看>>
(二十八)static关键字
查看>>
转 MySQL数据库基础
查看>>
ubuntu 解压命令全部
查看>>
Chrome教程(一)NetWork面板分析网络请求
查看>>
第十八回  基础才是重中之重~开发人员应学会用throw
查看>>
Swift -- 中文版两大官方文档汇总
查看>>
U3D调用7z解压文件
查看>>
Windows移动开发(二)——闭关修炼
查看>>
java 获取 path
查看>>
小盆友给谷歌写封信 老爸获一周假期
查看>>
Ubuntu安装配置Qt环境
查看>>
LBS 与 GPS 定位之间的区别
查看>>
Android调用系统的Activity、ContentProvider、Service、Broadcast Receiver
查看>>