一、题目概述
这个题是困难的题,第一眼看到这个题还是比较怕的,但是看了一下具体的内容,感觉还好,但是我有点弄不明白的是,英文里最大计数单位是多少,有没有比trillion更大的。
然后我百度搜了一下,最大的单位原来是billion。。。那我之前学的是假英语?
还有就是今天做这个题,让我对英文中数字分段有了个更清晰的认识,
比如这个图片,他的数字是三个三个分开的,因为英语里没有“万”的概念,说的都是“十千”,然后千的上面就是百万了,然后十亿。这么看,还是中华文化博大精深啊hhhh,每个位数都有它自己的名字。
二、个人思路
我的代码巨长,而且超时了,我暂且粘在这里,大家感兴趣的可以看一下,不想看的话我后面会解释一下。
class Solution {
public:
string numberToWords(int num) {
vector<int> ram;
int bit=0;
while(num>10){
bit=num%10;
ram.push_back(bit);
num/=10;
}//得到一个倒过来的那个数字的序列
vector<string> sent;
bit=1;
while(bit<ram.size()){
if(bit==2||bit==5||bit==8){
switch(ram[bit-1]){
case 1:{
switch(ram[bit-2]){
case 1:{
sent.insert(sent.begin(),"eleven");
break;
}
case 2:{
sent.insert(sent.begin(),"twelve");
break;
}
case 3:{
sent.insert(sent.begin(),"thirteen");
break;
}
case 4:{
sent.insert(sent.begin(),"fourteen");
break;
}
case 5:{
sent.insert(sent.begin(),"fifteen");
break;
}
case 6:{
sent.insert(sent.begin(),"sixteen");
break;
}
case 7:{
sent.insert(sent.begin(),"seventeen");
break;
}
case 8:{
sent.insert(sent.begin(),"eighteen");
break;
}
case 9:{
sent.insert(sent.begin(),"ninteen");
break;
}
case 0:{
sent.insert(sent.begin(),"ten");
break;
}
}
}
case 2:{
sent.insert(sent.begin(),"twenty");
break;
}
case 3:{
sent.insert(sent.begin(),"thirty");
break;
}
case 4:{
sent.insert(sent.begin(),"fourty");
break;
}
case 5:{
sent.insert(sent.begin(),"fifty");
break;
}
case 6:{
sent.insert(sent.begin(),"sixty");
break;
}
case 7:{
sent.insert(sent.begin(),"seventy");
break;
}
case 8:{
sent.insert(sent.begin(),"eighty");
break;
}
case 9:{
sent.insert(sent.begin(),"nighty");
break;
}
case 0:{
break;
}
}
}
if(bit==4){
sent.insert(sent.begin(),"thousand");
}else if(bit==7){
sent.insert(sent.begin(),"million");
}else if(bit==10){
sent.insert(sent.begin(),"billion");
}
switch(ram[bit-1]){
case 1:{
sent.insert(sent.begin(),"one");
break;
}
case 2:{
sent.insert(sent.begin(),"two");
break;
}case 3:{
sent.insert(sent.begin(),"three");
break;
}case 4:{
sent.insert(sent.begin(),"four");
break;
}case 5:{
sent.insert(sent.begin(),"five");
break;
}case 6:{
sent.insert(sent.begin(),"six");
break;
}case 7:{
sent.insert(sent.begin(),"seven");
break;
}case 8:{
sent.insert(sent.begin(),"eight");
break;
}case 9:{
sent.insert(sent.begin(),"nine");
break;
}case 0:{
break;
}
}
}
string str=sent[0];
for(int i=1;i<sent.size();i++){
str+=sent[i];
}
return str;
}
};
写了144行,我这个是分类讨论的思想,首先我们找到英语中说数据的特性,三个一组,每个三人组都是“几百几十几”这样来的,那我们就每三个一分类,需要注意的是,百位和个位可以直接说,但是十位数不行,十位数需要说成是twenty,thirty这种。然后十位数是 1 的话,他和个位还要说成是eleven,twelve这样子。然后每三位都加一个thousand,million,billion,我的思路就是这样。思路应该是没问题的,就是时间复杂度太高了。
中间有一个小插曲:我在写insert函数的时候写错了,我在下面把insert函数我写的错的那版和正确的版本都写在下面,大家引以为戒。
vector<int> sent;
错误的:↓
sent.insert(500,0); 我的意思是把500插入到sent的第一个位置
正确的:↓
sent.insert(sent.begin(),500); 正确的意思是,在sent的begin位置,插入500
三、大佬思路
网上对这个题意见很大,因为代码简单,就是过程复杂,写着费劲,我再同意不过了,但是作为一个编码小白,我也不敢说什么,因为我总觉得会有简单的方法,是我自己写复杂了。后面看了一下大佬代码,这个题确实很麻烦。。。
官方题解的第一个方法是递归,代码如下:
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 = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
vector<string> thousands = {"", "Thousand", "Million", "Billion"};
string numberToWords(int num) {
if (num == 0) {
return "Zero";
}
string sb;
for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) {
int curNum = num / unit;
if (curNum != 0) {
num -= curNum * unit;
string curr;
recursion(curr, curNum);
curr = curr + thousands[i] + " ";
sb = sb + curr;
}
}
while (sb.back() == ' ') {
sb.pop_back();
}
return sb;
}
void recursion(string & curr, int num) {
if (num == 0) {
return;
} else if (num < 10) {
curr = curr + singles[num] + " ";
} else if (num < 20) {
curr = curr + teens[num - 10] + " ";
} else if (num < 100) {
curr = curr + tens[num / 10] + " ";
recursion(curr, num % 10);
} else {
curr = curr + singles[num / 100] + " Hundred ";
recursion(curr, num % 100);
}
}
};
时间复杂度:O(1)。非负整数 nums 按照 3 位一组划分最多有 4 组,分别得到每一组的英文表示,然后拼接得到整数 num 的英文表示,时间复杂度是常数。
空间复杂度:O(1)。空间复杂度主要取决于存储英文表示的字符串和递归调用栈,英文表示的长度可以看成常数,递归调用栈不会超过 3 层。
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/integer-to-english-words/solution/zheng-shu-zhuan-huan-ying-wen-biao-shi-b-ivik/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我大概能看懂这个方法,他也是跟我一样,分成几个三元组,然后把十位数,或者十几这种数字单独讨论,就是写的高大上一点,和我内核是一样的。
这里提一下,pop_back是删除最后一个数据。
然后还有一个迭代方法,意思就是完全一样,就是换了种写法。
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 = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
vector<string> thousands = {"", "Thousand", "Million", "Billion"};
string numberToWords(int num) {
if (num == 0) {
return "Zero";
}
string sb;
for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) {
int curNum = num / unit;
if (curNum != 0) {
num -= curNum * unit;
sb = sb + toEnglish(curNum) + thousands[i] + " ";
}
}
while (sb.back() == ' ') {
sb.pop_back();
}
return sb;
}
string toEnglish(int num) {
string curr;
int hundred = num / 100;
num %= 100;
if (hundred != 0) {
curr = curr + singles[hundred] + " Hundred ";
}
int ten = num / 10;
if (ten >= 2) {
curr = curr + tens[ten] + " ";
num %= 10;
}
if (num > 0 && num < 10) {
curr = curr + singles[num] + " ";
} else if (num >= 10) {
curr = curr + teens[num - 10] + " ";
}
return curr;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/integer-to-english-words/solution/zheng-shu-zhuan-huan-ying-wen-biao-shi-b-ivik/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
四、总结
这个真没什么好总结的,就是复习了一下insert,然后学了个新的pop_back,大概就是这样。
看完题解,感觉这个题确实是没什么意思,我也不评价了。