LeetCode-Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

这道题看起来十分弱小,吃掉它

class Solution {
public:
	vector<string> letterCombinations(string digits) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		vector<string> ret;
		if(digits.length()==0){
		    ret.push_back("");
		    return ret;
		}
		map<char,string> m;
		m['2']="abc";
		m['3']="def";
		m['4']="ghi";
		m['5']="jkl";
		m['6']="mno";
		m['7']="pqrs";
		m['8']="tuv";
		m['9']="wxyz";
		m['0']=" ";
		m['*']="+";
		vector<int> ptr;
		ptr.resize(digits.length());
		for(int i=0;i<ptr.size();i++)ptr[i]=0;
		int i=0;
		string rs;
		for(;;){
			string s=m[digits[i]];
			if(ptr[i]==s.length()){
				i--;
				if(i<0)break;
				rs.resize(i);
			}
			else{
				rs+=s[ptr[i]];
				ptr[i]++;
				i++;
				if(i>=digits.length()){
					ret.push_back(rs);
					i--;
					rs.resize(i);
				}
				else{
					ptr[i]=0;
				}
			}
		}
		return ret;
	}
};
原文地址:https://www.cnblogs.com/superzrx/p/3330595.html