017. Letter Combinations of a Phone Number
题目大意:对于一个给出的数字串,输出其可能的在手机键盘上代表的字符串,如对于数字串“23”。
其可能代表的字符串便有”ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”
题目链接
17. Letter Combinations of a Phone Number
switch
一个for一个switch,应该可以解决问题。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
148class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> two = { "a","b","c" };
vector<string> three = { "d","e","f" };
vector<string> four = { "g","h","i" };
vector<string> five = { "j","k","l" };
vector<string> six = { "m","n","o" };
vector<string> seven = { "p","q","r","s" };
vector<string> eight = { "t","u","v" };
vector<string> nine = { "w","x","y","z" };
vector<string> answer = {};
vector<string> temp = {};
for (int i = 0; i < digits.size(); i++) {
switch (digits[i])
{
case '1':
break;
case '0':
break;
case '2': {
if (!answer.size()) {
for (int i = 0; i < two.size(); i++)
answer.push_back(two[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < two.size(); j++) {
answer.push_back(temp[i] + two[j]);
}
}
break;
}
case '3': {
if (!answer.size()) {
for (int i = 0; i < three.size(); i++)
answer.push_back(three[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < three.size(); j++) {
answer.push_back(temp[i] + three[j]);
}
}
break;
}
case '4': {
if (!answer.size()) {
for (int i = 0; i < four.size(); i++)
answer.push_back(four[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < four.size(); j++) {
answer.push_back(temp[i] + four[j]);
}
}
break;
}
case '5': {
if (!answer.size()) {
for (int i = 0; i < five.size(); i++)
answer.push_back(five[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < five.size(); j++) {
answer.push_back(temp[i] + five[j]);
}
}
break;
}
case '6': {
if (!answer.size()) {
for (int i = 0; i < six.size(); i++)
answer.push_back(six[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < six.size(); j++) {
answer.push_back(temp[i] + six[j]);
}
}
break;
}
case '7': {
if (!answer.size()) {
for (int i = 0; i < seven.size(); i++)
answer.push_back(seven[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < seven.size(); j++) {
answer.push_back(temp[i] + seven[j]);
}
}
break;
}
case '8': {
if (!answer.size()) {
for (int i = 0; i < eight.size(); i++)
answer.push_back(eight[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < eight.size(); j++) {
answer.push_back(temp[i] + eight[j]);
}
}
break;
}
case '9': {
if (!answer.size()) {
for (int i = 0; i < nine.size(); i++)
answer.push_back(nine[i]);
break;
}
temp = answer;
answer.clear();
for (int i = 0; i < temp.size(); i++) {
for (int j = 0; j < nine.size(); j++) {
answer.push_back(temp[i] + nine[j]);
}
}
break;
}
default:
break;
}
}
return answer;
}
};
代码看起来挺奇怪的。。。。一点没有刷leetcode的快感。
略微洋气
1 | class Solution { |
017. Letter Combinations of a Phone Number