首页 > 其他分享 >leetcode 273. 整数转换英文表示 困难

leetcode 273. 整数转换英文表示 困难

时间:2024-11-14 15:23:06浏览次数:1  
标签:return num else 273 整数 英文 转换 leetcode

273. 整数转换英文表示

这道题并不难,但是特别麻烦

我写的代码

class Solution {
public:
    //转换个位数的英文
    string baseNumber(int num){
        if(num == 1)  return "One";
        else if(num == 2)  return "Two";
        else if(num == 3)  return "Three";
        else if(num == 4)  return "Four";
        else if(num == 5)  return "Five";
        else if(num == 6)  return "Six";
        else if(num == 7)  return "Seven";
        else if(num == 8)  return "Eight";
        else if(num == 9)  return "Nine";
        else  return "";  //个位是0,返回空串
    }
    //转换两位数的英文
    string tenNumber(int num){
        if(num == 0)  return "";  //两位数都是0,返回空串
        if(num >= 10 && num <= 19){
            if(num == 10)  return "Ten";
            else if(num == 11)  return "Eleven";
            else if(num == 12)  return "Twelve";
            else if(num == 13)  return "Thirteen";
            else if(num == 14)  return "Fourteen";
            else if(num == 15)  return "Fifteen";
            else if(num == 16)  return "Sixteen";
            else if(num == 17)  return "Seventeen";
            else if(num == 18)  return "Eighteen";
            else if(num == 19)  return "Nineteen";
        }
        else if(num <= 9)  return baseNumber(num);
        //接下来的是20-99的转换
        int ten = num/10;
        int base = num%10;
        string res = "";
        //twenty thirty fourty fifty sixty seventy eighty ninety
        if(ten == 2)        res+="Twenty";
        else if(ten == 3)  res+="Thirty";
        else if(ten == 4)  res+="Forty";
        else if(ten == 5)  res+="Fifty";
        else if(ten == 6)  res+="Sixty";
        else if(ten == 7)  res+="Seventy";
        else if(ten == 8)  res+="Eighty";
        else if(ten == 9)  res+="Ninety";
        if(base == 0)  return res;
        return res + " " + baseNumber(base);
    }
    //转换三位数的英文
    string hundredNumber(int num){
        if(num == 0)  return ""; //三位数都是0,返回空串
        if(num < 100) return tenNumber(num);
        if(num % 100 == 0) return baseNumber(num/100) + " " + "Hundred";
        return baseNumber(num / 100) + " " + "Hundred" + " " + tenNumber(num % 100);  
    }
    string numberToWords(int num) {
        if(num == 0) return "Zero";
        string res="";

        //hundred 100 thousand 1000 million 1000000 billion 1000000000
        int billion = num / 1000000000;
        if(billion != 0)  res = res + baseNumber(billion) + " " + "Billion" + " ";
        num = num % 1000000000;

        int million = num / 1000000;
        if(million != 0)  res = res + hundredNumber(million) + " " + "Million" + " ";
        num = num % 1000000;

        int thousand = num / 1000;
        if(thousand != 0)  res = res + hundredNumber(thousand) + " " + "Thousand" + " ";
        num = num % 1000;
        
        res = res + hundredNumber(num);
        if(res[res.size()-1] == ' ')  res.pop_back();//如果最后有多余的空格就去掉

        return res;
    }
};

 

 

标签:return,num,else,273,整数,英文,转换,leetcode
From: https://www.cnblogs.com/uacs2024/p/18546048

相关文章

  • LeetCode刷题笔记9.9-9.15
    LeetCode刷题笔记9.9-9.15二叉树主要学两种遍历方式:层序遍历、递归遍历1)层序遍历BFS基本思想:逐层遍历元素,可以借助队列,先进先出,队首出元素的同时进该元素的左右节点(这也是最简单的实现方式)队列Q:1->出1进2,3(2,3)->出2进4(3,4)->出3进5,6(4,5,6)->出4(5,6)->出5(6)->出6(空)队列进......
  • [LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store
    Youaregivenanintegernindicatingtherearenspecialtyretailstores.Therearemproducttypesofvaryingamounts,whicharegivenasa0-indexedintegerarrayquantities,wherequantities[i]representsthenumberofproductsoftheithproducttype......
  • LeetCode 1103[分糖果II]
    题目链接LeetCode1103[分糖果II]详情实例提示题解思路定义容器vecRet,使每个元素值均为0,即代表每个孩子手上开始都是0个糖果定义iCount为默认的糖果数量,初始值为1逐个遍历容器,也就是开始给每个孩子分糖果获取容器当前元素值,即每个孩子当前的糖果数量iAt如果糖果......
  • LeetCode【0046】全排列
    本文目录1中文题目2求解方法:回溯法2.1方法思路2.2Python代码2.3复杂度分析3题目总结1中文题目给定一个不含重复数字的数组nums,返回其所有可能的全排列。可以按任意顺序返回答案。示例:输入:nums=[1,2,3]输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,......
  • [LeetCode] 1385. Find the Distance Value Between Two Arrays
    Giventwointegerarraysarr1andarr2,andtheintegerd,returnthedistancevaluebetweenthetwoarrays.Thedistancevalueisdefinedasthenumberofelementsarr1[i]suchthatthereisnotanyelementarr2[j]where|arr1[i]-arr2[j]|<=d.Exampl......
  • LeetCode 3341. Find Minimum Time to Reach Last Room I
    原题链接在这里:https://leetcode.com/problems/find-minimum-time-to-reach-last-room-i/description/题目:Thereisadungeonwith nxm roomsarrangedasagrid.Youaregivena2Darray moveTime ofsize nxm,where moveTime[i][j] representsthe minimum ......
  • 2024-11-13:求出所有子序列的能量和。用go语言,给定一个整数数组nums和一个正整数k, 定义
    2024-11-13:求出所有子序列的能量和。用go语言,给定一个整数数组nums和一个正整数k,定义一个子序列的能量为子序列中任意两个元素之间的差值绝对值的最小值。找出nums中长度为k的所有子序列的能量和,对结果取模10^9+7后返回。输入:nums=[1,2,3,4],k=3。输出:4。解释:......
  • RK3568平台开发系列讲解(GPIO篇)基于整数的GPIO接口
    ......
  • 代码随想录算法训练营第二十五天| leetcode491.递增子序列、leetcode46.全排列、leetc
    1leetcode491.递增子序列题目链接:491.非递减子序列-力扣(LeetCode)文章链接:代码随想录视频链接:回溯算法精讲,树层去重与树枝去重|LeetCode:491.递增子序列_哔哩哔哩_bilibili思路:用之前的方法,结果翻车了,好好看视频学新技能吧1.1视频后的思路真的没想到用set来去重,还是基......
  • 2024-11-13:求出所有子序列的能量和。用go语言,给定一个整数数组nums和一个正整数k, 定义
    2024-11-13:求出所有子序列的能量和。用go语言,给定一个整数数组nums和一个正整数k,定义一个子序列的能量为子序列中任意两个元素之间的差值绝对值的最小值。找出nums中长度为k的所有子序列的能量和,对结果取模10^9+7后返回。输入:nums=[1,2,3,4],k=3。输出:4。解释:nums......