forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetter_Combinations_of_a_Phone_Number.cpp
More file actions
58 lines (53 loc) · 1.2 KB
/
Letter_Combinations_of_a_Phone_Number.cpp
File metadata and controls
58 lines (53 loc) · 1.2 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
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 1, 2012
Problem: Letter Combinations of a Phone Number
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given a digit string, return all possible letter combinations that the number
could represent.
Solution:
simple.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
string arr[10];
vector<string> result;
void recursion(string digits, unsigned n, string str) {
if (n >= digits.length()) {
result.push_back(str);
return;
}
for (unsigned i = 0; i < arr[digits[n] - '0'].length(); i++) {
recursion(digits, n + 1, str + arr[digits[n] - '0'][i]);
}
}
vector<string> letterCombinations(string digits) {
result.clear();
for (int i= 0; i< 10; i++) {
arr[i].clear();
}
arr[0] = " ";
arr[1] = "";
arr[2] = "abc";
arr[3] = "def";
arr[4] = "ghi";
arr[5] = "jkl";
arr[6] = "mno";
arr[7] = "pqrs";
arr[8] = "tuv";
arr[9] = "wxyz";
recursion(digits, 0, "");
return result;
}
};