1. 程式人生 > >回溯Leetcode 17 Letter Combinations of a Phone Number

回溯Leetcode 17 Letter Combinations of a Phone Number

Leetcode 17

Letter Combinations of a Phone Number

class Solution {
public
: vector<string> letterCombinations(string digits) { vector<string> res; if (digits.length() == 0) return res; res.push_back(""); vector<string> temp; vector<string> Phone={"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
}; for(int i = 0; i < digits.length(); i++) { int num = digits[i]-'0'; if(num<0||num>9) break; string t = Phone[num]; if (t.length() == 0) continue; for (int k = 0;k < t.length(); k++) { for (int j = 0; j < res.size(); j++) { temp.push_back(res[j]+t[k]); } } res.swap(temp); temp.clear(); } return
res; } };