zoukankan      html  css  js  c++  java
  • 【LeetCode】273. Integer to English Words

    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"

    Show Hint 

       先把数字分割成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;
              }
          }
      };

    1. 相关阅读:
      C programming course
      关于时间管理的培训心得
      吴老师,一路好走!
      自己实现Int32Collection(.Net 1.1),以及效率问题的体会
      《C陷阱与缺陷》和《C专家编程》两本书又翻印了
      暂时闲一会,写一点点面试体会吧
      人类没有一件事是值得烦恼的
      EP0N系统中简便可行的光纤保护方法
      越来越不想写代码了
      [转]PON关键技术-通用成帧协议研究
    2. 原文地址:https://www.cnblogs.com/ganganloveu/p/5319371.html
    Copyright © 2011-2022 走看看