您的位置:首页 > 职场人生

[LeetCode] Letter Combinations of a Phone Number

2013-01-28 18:36 393 查看
class Solution {
public:
vector<string> map;
vector<string> ret;
vector<char> sln;

Solution() {
map.push_back("");
map.push_back("");
map.push_back("abc");
map.push_back("def");
map.push_back("ghi");
map.push_back("jkl");
map.push_back("mno");
map.push_back("pqrs");
map.push_back("tuv");
map.push_back("wxyz");
}

void DFS(string digits, int n) {
if (n == digits.size()) {
string s = "";
for (int i = 0; i < sln.size(); i++)
s += sln[i];

ret.push_back(s);
return;
}

int pos = digits
- '0';

for (int i = 0; i < map[pos].size(); i++) {
sln.push_back(map[pos][i]);
DFS(digits, n + 1);
sln.pop_back();
}
}

vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
DFS(digits, 0);
return ret;
}
};


Small Case: 8ms

Large Case: 0ms
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息