Skip to content

Commit 43b0787

Browse files
committed
New Problem "Integer to English Words"
1 parent 68b953b commit 43b0787

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ LeetCode
1313
|285|[Inorder Successor in BST](https://leetcode.com/problems/inorder-successor-in-bst/) ♥ | [Java](./algorithms/java/src/inorderSuccessorInBST/inorderSuccessorInBST.java)|Medium|
1414
|283|[Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./algorithms/cpp/moveZeroes/moveZeroes.cpp)|Easy|
1515
|278|[First Bad Version](https://leetcode.com/problems/first-bad-version/)| [C++](./algorithms/cpp/firstBadVersion/FirstBadVersion.cpp), [Java](./algorithms/java/src/firstBadVersion/firstBadVersion.java)|Easy|
16+
|273|[Integer to English Words](https://leetcode.com/problems/integer-to-english-words/)| [C++](./algorithms/cpp/integerToEnglishWords/IntegerToEnglishWords.cpp)|Medium|
1617
|268|[Missing Number](https://leetcode.com/problems/missing-number/)| [C++](./algorithms/cpp/missingNumber/MissingNumber.cpp)|Medium|
1718
|264|[Ugly Number II](https://leetcode.com/problems/ugly-number/)| [C++](./algorithms/cpp/uglyNumber/UglyNumber.II.cpp)|Medium|
1819
|263|[Ugly Number](https://leetcode.com/problems/ugly-number/)| [C++](./algorithms/cpp/uglyNumber/UglyNumber.cpp)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Source : https://leetcode.com/problems/integer-to-english-words/
2+
// Author : Hao Chen
3+
// Date : 2015-10-22
4+
5+
/***************************************************************************************
6+
*
7+
* Convert a non-negative integer to its english words representation. Given input is
8+
* guaranteed to be less than 231 - 1.
9+
*
10+
* For example,
11+
*
12+
* 123 -> "One Hundred Twenty Three"
13+
* 12345 -> "Twelve Thousand Three Hundred Forty Five"
14+
* 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
15+
*
16+
* Did you see a pattern in dividing the number into chunk of words? For example, 123
17+
* and 123000.
18+
*
19+
* Group the number by thousands (3 digits). You can write a helper function that
20+
* takes a number less than 1000 and convert just that chunk to words.
21+
*
22+
* There are many edge cases. What are some good test cases? Does your code work with
23+
* input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
24+
*
25+
***************************************************************************************/
26+
27+
#include <stdlib.h>
28+
#include <iostream>
29+
#include <vector>
30+
#include <string>
31+
using namespace std;
32+
33+
static string dict1[] ={"Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
34+
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
35+
"Seventeen", "Eighteen", "Nineteen"};
36+
37+
static string dict2[]={"","", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
38+
static string dict3[]={"Hundred", "Thousand", "Million", "Billion" };
39+
40+
41+
// This function only converts the number which less than 1000
42+
string numberLess1000ToWords(int num) {
43+
//char n[3] = {0, 0, 0};
44+
string result;
45+
46+
if (num == 0) {
47+
return result;
48+
}else if (num < 20) {
49+
return dict1[num];
50+
} else if (num < 100) {
51+
result = dict2[num/10];
52+
if (num%10 > 0) {
53+
result += " " + dict1[num%10];
54+
}
55+
}else {
56+
result = dict1[num/100] + " " + dict3[0];
57+
if ( num % 100 > 0 ) {
58+
result += " " + numberLess1000ToWords( num % 100 );
59+
}
60+
}
61+
return result;
62+
}
63+
64+
string numberToWords(int num) {
65+
//edge case
66+
if (num ==0 ) return dict1[num];
67+
68+
vector<string> ret;
69+
for( ;num > 0; num/=1000 ) {
70+
ret.push_back( numberLess1000ToWords(num % 1000) );
71+
}
72+
73+
string result=ret[0];
74+
for (int i=1; i<ret.size(); i++){
75+
if (ret[i].size() > 0 ){
76+
if ( result.size() > 0 ) {
77+
result = ret[i] + " " + dict3[i] + " " + result;
78+
} else {
79+
result = ret[i] + " " + dict3[i];
80+
}
81+
}
82+
83+
}
84+
return result;
85+
}
86+
87+
88+
#define TEST(num) cout << num << " -> \"" << numberToWords(num) << "\"" << endl
89+
int main(int argc, char** argv)
90+
{
91+
int num = 123;
92+
if (argc >1){
93+
num = atoi(argv[1]);
94+
}
95+
TEST(num);
96+
97+
TEST(0);
98+
TEST(1);
99+
TEST(10);
100+
TEST(11);
101+
TEST(18);
102+
TEST(20);
103+
TEST(22);
104+
TEST(30);
105+
TEST(99);
106+
TEST(100);
107+
TEST(101);
108+
TEST(110);
109+
TEST(120);
110+
TEST(256);
111+
TEST(1000);
112+
TEST(1001);
113+
TEST(1010);
114+
TEST(1110);
115+
TEST(1111);
116+
TEST(10000);
117+
TEST(10001);
118+
TEST(100000);
119+
TEST(100001);
120+
TEST(1000000);
121+
TEST(1000001);
122+
TEST(10000000);
123+
TEST(10000001);
124+
TEST(100000000);
125+
TEST(100000001);
126+
TEST(1000000000);
127+
TEST(1000000001);
128+
TEST(2147483647);
129+
130+
return 0;
131+
}

0 commit comments

Comments
 (0)