-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46_TranslateNumbersToStrings.cpp
More file actions
202 lines (171 loc) · 4.11 KB
/
46_TranslateNumbersToStrings.cpp
File metadata and controls
202 lines (171 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*******************************************************************
Copyright(c) 2016, Harry He
All rights reserved.
Distributed under the BSD license.
(See accompanying file LICENSE.txt at
https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
*******************************************************************/
//==================================================================
// 《剑指Offer——名企面试官精讲典型编程题》代码
// 作者:何海涛
//==================================================================
// 面试题46:把数字翻译成字符串
// 题目:给定一个数字,我们按照如下规则把它翻译为字符串:0翻译成"a",1翻
// 译成"b",……,11翻译成"l",……,25翻译成"z"。一个数字可能有多个翻译。例
// 如12258有5种不同的翻译,它们分别是"bccfi"、"bwfi"、"bczi"、"mcfi"和
// "mzi"。请编程实现一个函数用来计算一个数字有多少种不同的翻译方法。
#include <string>
#include <iostream>
using namespace std;
#if 0
int GetTranslationCount(const string& number);
int GetTranslationCount(int number)
{
if(number < 0)
return 0;
string numberInString = to_string(number);
return GetTranslationCount(numberInString);
}
int GetTranslationCount(const string& number)
{
int length = number.length();
int* counts = new int[length];
int count = 0;
for(int i = 0; i < length; i++)
{
if(i == 0)
counts[i] = 1;
else if(i == 1)
{
if(number[0] == '1' || (number[0] == '2' && number[1] >= '0' && number[1] <= '5'))
counts[i] = 2;
else
counts[i] = 1;
}
else
{
if(number[i - 1] == '1' || (number[i - 1] == '2' && number[i] >= '0' && number[i] <= '5'))
counts[i] = counts[i - 2] + counts[i - 1];
else
counts[i] = counts[i - 1];
}
count = counts[i];
}
delete[] counts;
return count;
}
#endif
int GetTranslationCount(const string& number);
int GetTranslationCount(int number)
{
if(number < 0)
return 0;
string numberInString = to_string(number);
return GetTranslationCount(numberInString);
}
int GetTranslationCount(const string& number)
{
int length = number.length();
int* counts = new int[length];
int count = 0;
for(int i = length - 1; i >= 0; --i)
{
count = 0;
if(i < length - 1)
count = counts[i + 1];
else
count = 1;
if(i < length - 1)
{
int digit1 = number[i] - '0';
int digit2 = number[i + 1] - '0';
int converted = digit1 * 10 + digit2;
if(converted >= 10 && converted <= 25)
{
if(i < length - 2)
count += counts[i + 2];
else
count += 1;
}
}
counts[i] = count;
}
count = counts[0];
delete[] counts;
return count;
}
// ====================测试代码====================
void Test(const string& testName, int number, int expected)
{
if(GetTranslationCount(number) == expected)
cout << testName << " passed." << endl;
else
cout << testName << " FAILED." << endl;
}
void Test1()
{
int number = 0;
int expected = 1;
Test("Test1", number, expected);
}
void Test2()
{
int number = 10;
int expected = 2;
Test("Test2", number, expected);
}
void Test3()
{
int number = 125;
int expected = 3;
Test("Test3", number, expected);
}
void Test4()
{
int number = 126;
int expected = 2;
Test("Test4", number, expected);
}
void Test5()
{
int number = 426;
int expected = 1;
Test("Test5", number, expected);
}
void Test6()
{
int number = 100;
int expected = 2;
Test("Test6", number, expected);
}
void Test7()
{
int number = 101;
int expected = 2;
Test("Test7", number, expected);
}
void Test8()
{
int number = 12258;
int expected = 5;
Test("Test8", number, expected);
}
void Test9()
{
int number = -100;
int expected = 0;
Test("Test9", number, expected);
}
int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
return 0;
}